Sophie

Sophie

distrib > Mageia > 4 > x86_64 > by-pkgid > f800694edefe91adea2624f711a41a2d > files > 8885

php-manual-en-5.5.7-1.mga4.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="example" id="example-66">
     <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 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">&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 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, &lt;?php ?&gt; and
    &lt;script language=&quot;php&quot;&gt; &lt;/script&gt;, 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 &lt;?php ?&gt; 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.&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;XHTML&nbsp;or&nbsp;XML&nbsp;documents,&nbsp;do&nbsp;it&nbsp;like&nbsp;this'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span><br />2.&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'</span><span style="color: #007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">&lt;/script&gt;<br /></span><br />3.&nbsp;&nbsp;<span style="color: #0000BB">&lt;?&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #DD0000">'this&nbsp;is&nbsp;the&nbsp;simplest,&nbsp;an&nbsp;SGML&nbsp;processing&nbsp;instruction'</span><span style="color: #007700">;&nbsp;</span><span style="color: #0000BB">?&gt;<br /></span>&nbsp;&nbsp;&nbsp;&nbsp;<span style="color: #0000BB">&lt;?=&nbsp;expression&nbsp;?&gt;</span>&nbsp;This&nbsp;is&nbsp;a&nbsp;shortcut&nbsp;for&nbsp;"<span style="color: #0000BB">&lt;?&nbsp;</span><span style="color: #007700">echo&nbsp;</span><span style="color: #0000BB">expression&nbsp;?&gt;</span>"<br /><br />4.&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;&lt;%=&nbsp;$variable;&nbsp;#&nbsp;This&nbsp;is&nbsp;a&nbsp;shortcut&nbsp;for&nbsp;"&lt;%&nbsp;echo&nbsp;.&nbsp;.&nbsp;."&nbsp;%&gt;</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>&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>