<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Escaping from HTML</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.basic-syntax.phptags.html">PHP tags</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="language.basic-syntax.html">Basic syntax</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="language.basic-syntax.phpmode" class="sect1"> <h2 class="title">Escaping from HTML</h2> <p class="para"> Everything outside of a pair of opening and closing tags is ignored by the PHP parser which allows PHP files to have mixed content. This allows PHP to be embedded in HTML documents, for example to create templates. <div class="example" id="example-66"> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <p>This is going to be ignored by PHP and displayed by the browser.</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 by PHP and displayed by the browser.</p></span> </code></div> </div> </div> This works as expected, because when the PHP interpreter 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 unless in the middle of a conditional statement in which case the interpreter will determine the outcome of the conditional before making a decision of what which to skip over. See the next example. </p> <p class="para"> Using structures with conditions <div class="example" id="example-67"> <p><strong>Example #1 Advanced escaping using conditions</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php </span><span style="color: #007700">if (</span><span style="color: #0000BB">$expression </span><span style="color: #007700">== </span><span style="color: #0000BB">true</span><span style="color: #007700">): </span><span style="color: #0000BB">?><br /></span> This will show if the expression is true.<br /><span style="color: #0000BB"><?php </span><span style="color: #007700">else: </span><span style="color: #0000BB">?><br /></span> Otherwise this will show.<br /><span style="color: #0000BB"><?php </span><span style="color: #007700">endif; </span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> In this example PHP will skip the blocks where the condition is not met, even though they are outside of the PHP open/close tags, PHP skips them according to the condition since the PHP interpreter will jump over blocks contained within a condition what is not met. </p> <p class="para"> For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through <span class="function"><a href="function.echo.html" class="function">echo</a></span> or <span class="function"><a href="function.print.html" class="function">print</a></span>. </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 class="note"><p><strong class="note">Note</strong>: <p class="para"> 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. </p> </p></blockquote> </p> <p class="para"> <div class="example" id="example-68"> <p><strong>Example #2 PHP Opening and Closing Tags</strong></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 it 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 <strong class="option unknown">--enable-short-tags</strong> 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 class="note"><p><strong class="note">Note</strong>: <p class="para"> 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. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> In PHP 5.2 and earlier, the parser does not allow the <em><?php</em> opening tag to be the only thing in a file. This is allowed as of PHP 5.3 provided there are one or more whitespace characters after the opening tag. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Starting with PHP 5.4, short echo tag <em><?=</em> is always recognized and valid, regardless of the <a href="ini.core.html#ini.short-open-tag" class="link">short_open_tag</a> setting. </p> </p></blockquote> </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.basic-syntax.phptags.html">PHP tags</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="language.basic-syntax.html">Basic syntax</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>