Sophie

Sophie

distrib > Mageia > 7 > i586 > by-pkgid > 2b917e0437961edec048f1d15e2d7449 > files > 9377

php-manual-en-7.2.11-1.mga7.noarch.rpm

<!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">
&lt;p&gt;This&nbsp;is&nbsp;going&nbsp;to&nbsp;be&nbsp;ignored&nbsp;by&nbsp;PHP&nbsp;and&nbsp;displayed&nbsp;by&nbsp;the&nbsp;browser.&lt;/p&gt;<br /><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'While&nbsp;this&nbsp;is&nbsp;going&nbsp;to&nbsp;be&nbsp;parsed.'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&lt;p&gt;This&nbsp;will&nbsp;also&nbsp;be&nbsp;ignored&nbsp;by&nbsp;PHP&nbsp;and&nbsp;displayed&nbsp;by&nbsp;the&nbsp;browser.&lt;/p&gt;</span>
</code></div>
     </div>

    </div>
    This works as expected, because when the PHP interpreter hits the ?&gt; 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">&lt;?php&nbsp;</span><span style="color: #007700">if&nbsp;(</span><span style="color: #0000BB">$expression&nbsp;</span><span style="color: #007700">==&nbsp;</span><span style="color: #0000BB">true</span><span style="color: #007700">):&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&nbsp;&nbsp;This&nbsp;will&nbsp;show&nbsp;if&nbsp;the&nbsp;expression&nbsp;is&nbsp;true.<br /><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">else:&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&nbsp;&nbsp;Otherwise&nbsp;this&nbsp;will&nbsp;show.<br /><span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">endif;&nbsp;</span><span style="color: #0000BB">?&gt;</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">&lt;?php ?&gt;</code> and
    <code class="code">&lt;script language=&quot;php&quot;&gt; &lt;/script&gt;</code>, are always
    available. There is also the short echo tag <code class="code">&lt;?= ?&gt;</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 &lt;?php ?&gt; 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">&lt;script language=&quot;php&quot;&gt;</code> tags. As such, we recommend
    only using <code class="code">&lt;?php ?&gt;</code> and <code class="code">&lt;?= ?&gt;</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.&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'if&nbsp;you&nbsp;want&nbsp;to&nbsp;serve&nbsp;PHP&nbsp;code&nbsp;in&nbsp;XHTML&nbsp;or&nbsp;XML&nbsp;documents,<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;use&nbsp;these&nbsp;tags'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span><br />2.&nbsp;&nbsp;You&nbsp;can&nbsp;use&nbsp;the&nbsp;short&nbsp;echo&nbsp;tag&nbsp;to&nbsp;<span style="color: #0000BB">&lt;?=&nbsp;</span><span style="color: #DD0000">'print&nbsp;this&nbsp;string'&nbsp;</span><span style="color: #0000BB">?&gt;</span>.<br />&nbsp;&nbsp;&nbsp;&nbsp;It's&nbsp;always&nbsp;enabled&nbsp;in&nbsp;PHP&nbsp;5.4.0&nbsp;and&nbsp;later,&nbsp;and&nbsp;is&nbsp;equivalent&nbsp;to<br />&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?php&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'print&nbsp;this&nbsp;string'&nbsp;</span><span style="color: #0000BB">?&gt;</span>.<br /><br />3.&nbsp;&nbsp;<span style="color: #0000BB">&lt;?&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'this&nbsp;code&nbsp;is&nbsp;within&nbsp;short&nbsp;tags,&nbsp;but&nbsp;will&nbsp;only&nbsp;work&nbsp;'</span><span style="color: #007700">.<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'if&nbsp;short_open_tag&nbsp;is&nbsp;enabled'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span><br />4.&nbsp;&nbsp;<span style="color: #0000BB">&lt;script&nbsp;language="php"&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'some&nbsp;editors&nbsp;(like&nbsp;FrontPage)&nbsp;don\'t<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;like&nbsp;processing&nbsp;instructions&nbsp;within&nbsp;these&nbsp;tags'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&lt;/script&gt;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;This&nbsp;syntax&nbsp;is&nbsp;removed&nbsp;in&nbsp;PHP&nbsp;7.0.0.<br /><br />5.&nbsp;&nbsp;&lt;%&nbsp;echo&nbsp;'You&nbsp;may&nbsp;optionally&nbsp;use&nbsp;ASP-style&nbsp;tags';&nbsp;%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;Code&nbsp;within&nbsp;these&nbsp;tags&nbsp;&lt;%=&nbsp;$variable;&nbsp;%&gt;&nbsp;is&nbsp;a&nbsp;shortcut&nbsp;for&nbsp;this&nbsp;code&nbsp;&lt;%&nbsp;echo&nbsp;$variable;&nbsp;%&gt;<br />&nbsp;&nbsp;&nbsp;&nbsp;Both&nbsp;of&nbsp;these&nbsp;syntaxes&nbsp;are&nbsp;removed&nbsp;in&nbsp;PHP&nbsp;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>&lt;?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>&lt;?=</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>