Source of: /manual/en/language.basic-syntax.phpmode.php
<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/language.basic-syntax.inc";
$setup = array (
'home' =>
array (
0 => 'index.php',
1 => 'PHP Manual',
),
'head' =>
array (
0 => 'UTF-8',
1 => 'en',
),
'this' =>
array (
0 => 'language.basic-syntax.phpmode.php',
1 => 'Escaping from HTML',
),
'up' =>
array (
0 => 'language.basic-syntax.php',
1 => 'Basic syntax',
),
'prev' =>
array (
0 => 'language.basic-syntax.php',
1 => 'Basic syntax',
),
'next' =>
array (
0 => 'language.basic-syntax.instruction-separation.php',
1 => 'Instruction separation',
),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);
manual_header();
?>
<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.
</p><div class="informalexample">
<div class="example-contents programlisting">
<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>
<p class="para">
You can also use more advanced structures:
</p><div class="example">
<p><b>Example #1 Advanced escaping</b></p>
<div class="example-contents programlisting">
<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><p>
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.php" 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.php" class="function">echo()</a> or
<a href="function.print.php" 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.
</p><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>
<p class="para">
</p><div class="example">
<p><b>Example #2 PHP Opening and Closing Tags</b></p>
<div class="example-contents programlisting">
<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>
<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.php#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.php#ini.asp-tags" class="link">asp_tags</a> <var class="filename">php.ini</var>
configuration file directive.
</p>
<p class="para">
</p><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>
</p>
</div><?php manual_footer(); ?>