<!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="informalexample"> <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 to skip over. See the next example. </p> <p class="para"> Using structures with conditions <div class="example" id="example-39"> <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 that 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"> In PHP 5, there are up to five different pairs of opening and closing tags available in PHP, depending on how PHP is configured. Two of these, <code class="code"><?php ?></code> and <code class="code"><script language="php"> </script></code>, are always available. There is also the short echo tag <code class="code"><?= ?></code>, which is always available in PHP 5.4.0 and later. </p> <p class="para"> The other two are short tags and <span class="productname">ASP</span> style tags. 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"> PHP 7 removes support for <span class="productname">ASP</span> tags and <code class="code"><script language="php"></code> tags. As such, we recommend only using <code class="code"><?php ?></code> and <code class="code"><?= ?></code> when writing PHP code to maximise compatibility. </p> <p class="para"> <div class="example" id="example-40"> <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 PHP code in XHTML or XML documents,<br /> use these tags'</span><span style="color: #007700">; </span><span style="color: #0000BB">?><br /></span><br />2. You can use the short echo tag to <span style="color: #0000BB"><?= </span><span style="color: #DD0000">'print this string' </span><span style="color: #0000BB">?></span>.<br /> It's always enabled in PHP 5.4.0 and later, and is equivalent to<br /> <span style="color: #0000BB"><?php </span><span style="color: #007700">echo </span><span style="color: #DD0000">'print this string' </span><span style="color: #0000BB">?></span>.<br /><br />3. <span style="color: #0000BB"><? </span><span style="color: #007700">echo </span><span style="color: #DD0000">'this code is within short tags, but will only work '</span><span style="color: #007700">.<br /> </span><span style="color: #DD0000">'if short_open_tag is enabled'</span><span style="color: #007700">; </span><span style="color: #0000BB">?><br /></span><br />4. <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 within these tags'</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB"></script><br /></span> This syntax is removed in PHP 7.0.0.<br /><br />5. <% echo 'You may optionally use ASP-style tags'; %><br /> Code within these tags <%= $variable; %> is a shortcut for this code <% echo $variable; %><br /> Both of these syntaxes are removed in PHP 7.0.0.</span> </code></div> </div> </div> </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 five) 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, and have been removed in PHP 7.0.0. </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>