📄 language.basic-syntax.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Basic syntax</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="langref.html">Language Reference</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.basic-syntax.instruction-separation.html">Instruction separation</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div> <h1>Basic syntax</h1><h2>Table of Contents</h2><ul class="chunklist chunklist_chapter"><li><a href="language.basic-syntax.instruction-separation.html">Instruction separation</a></li><li><a href="language.basic-syntax.comments.html">Comments</a></li></ul> <div id="language.basic-syntax.phpmode" class="sect1"> <h2 class="title">Escaping from HTML</h2> <p class="para"> When PHP parses a file, it looks for opening and closing tags, which tell PHP to start and stop interpreting the code between them. Parsing in this manner allows php to be embedded in all sorts of different documents, as everything outside of a pair of opening and closing tags is ignored by the PHP parser. Most of the time you will see php embedded in HTML documents, as in this example. <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><p>This is going to be ignored.</p><br /><span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">'While this is going to be parsed.'</span><span style="color: #007700">; </span><span style="color: #0000BB">?><br /></span><p>This will also be ignored.</p></span></code></div> </div> </div> </p> <p class="para"> You can also use more advanced structures: <div class="example"> <p><b>Example #1 Advanced escaping</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br /></span><span style="color: #007700">if (</span><span style="color: #0000BB">$expression</span><span style="color: #007700">) { <br /> </span><span style="color: #0000BB">?><br /></span> <strong>This is true.</strong><br /> <span style="color: #0000BB"><?php <br /></span><span style="color: #007700">} else { <br /> </span><span style="color: #0000BB">?><br /></span> <strong>This is false.</strong><br /> <span style="color: #0000BB"><?php <br /></span><span style="color: #007700">}<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds (except for an immediately following newline - see <a href="language.basic-syntax.instruction-separation.html" class="link">instruction separation</a> ) until it hits another opening tag. The example given here is contrived, of course, but for outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through <a href="function.echo.html" class="function">echo()</a> or <a href="function.print.html" class="function">print()</a>. </p> <p class="para"> There are four different pairs of opening and closing tags which can be used in php. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and <span class="productname">ASP</span> style tags, and can be turned on and off from the <var class="filename">php.ini</var> configuration file. As such, while some people find short tags and <span class="productname">ASP</span> style tags convenient, they are less portable, and generally not recommended. <blockquote><p><b class="note">Note</b>: Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards. <br /> </p></blockquote> </p> <p class="para"> <div class="example"> <p><b>Example #2 PHP Opening and Closing Tags</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000">1. <span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">'if you want to serve XHTML or XML documents, do like this'</span><span style="color: #007700">; </span><span style="color: #0000BB">?><br /></span><br />2. <span style="color: #0000BB"><script language="php"><br /> </span><span style="color: #007700">echo </span><span style="color: #DD0000">'some editors (like FrontPage) don\'t<br /> like processing instructions'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB"></script><br /></span><br />3. <span style="color: #0000BB"><? </span><span style="color: #007700">echo </span><span style="color: #DD0000">'this is the simplest, an SGML processing instruction'</span><span style="color: #007700">; </span><span style="color: #0000BB">?><br /></span> <span style="color: #0000BB"><?= expression ?></span> This is a shortcut for "<span style="color: #0000BB"><? </span><span style="color: #007700">echo </span><span style="color: #0000BB">expression ?></span>"<br /><br />4. <% echo 'You may optionally use ASP-style tags'; %><br /> <%= $variable; # This is a shortcut for "<% echo . . ." %></span></code></div> </div> </div> </p> <p class="para"> While the tags seen in examples one and two are both always available, example one is the most commonly used, and recommended, of the two. </p> <p class="para"> Short tags (example three) are only available when they are enabled via the <a href="ini.core.html#ini.short-open-tag" class="link">short_open_tag</a> <var class="filename">php.ini</var> configuration file directive, or if php was configured with the <span class="option">--enable-short-tags</span> option. </p> <p class="para"> <span class="productname">ASP</span> style tags (example four) are only available when they are enabled via the <a href="ini.core.html#ini.asp-tags" class="link">asp_tags</a> <var class="filename">php.ini</var> configuration file directive. </p> <p class="para"> <blockquote><p><b class="note">Note</b>: Using short tags should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags. <br /> </p></blockquote> </p> </div> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="langref.html">Language Reference</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.basic-syntax.instruction-separation.html">Instruction separation</a></div> <div class="up"><a href="langref.html">Language Reference</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -