downloads | documentation | faq | getting help | mailing lists | reporting bugs | sites | links | my 
search for in the  
<Language ReferenceInstruction separation>
view the version of this page
Last updated: Thu, 21 Aug 2003

Chapter 5. Basic syntax

Escaping from HTML

When PHP parses a file, it simply passes the text of the file through until it encounters one of the special tags which tell it to start interpreting the text as PHP code. The parser then executes all the code it finds, up until it runs into a PHP closing tag, which tells the parser to just start passing the text through again. This is the mechanism which allows you to embed PHP code inside HTML: everything outside the PHP tags is left utterly alone, while everything inside is parsed as code.

There are four sets of tags which can be used to denote blocks of PHP code. Of these, only two (<?php. . .?> and <script language="php">. . .</script>) are always available; the others can be turned on or off from the php.ini configuration file. While the short-form tags and ASP-style tags may be convenient, they are not as portable as the longer versions. Also, if you intend to embed PHP code in XML or XHTML, you will need to use the <?php. . .?> form to conform to the XML.

The tags supported by PHP are:

Example 5-1. Ways of escaping from HTML

1.  <?php echo("if you want to serve XHTML or XML documents, do like this\n"); ?>

2.  <? echo ("this is the simplest, an SGML processing instruction\n"); ?>
    <?= expression ?> This is a shortcut for "<? echo expression ?>"
3.  <script language="php">
        echo ("some editors (like FrontPage) don't
              like processing instructions");

4.  <% echo ("You may optionally use ASP-style tags"); %>
    <%= $variable; # This is a shortcut for "<% echo . . ." %>

The first way, <?php. . .?>, is the preferred method, as it allows the use of PHP in XML-conformant code such as XHTML.

The second way is not available always. Short tags are available only when they have been enabled. This can be done via the short_tags() function (PHP 3 only), by enabling the short_open_tag configuration setting in the PHP config file, or by compiling PHP with the --enable-short-tags option to configure. Even if it is enabled by default in php.ini-dist, use of short tags are discouraged.

The fourth way is only available if ASP-style tags have been enabled using the asp_tags configuration setting.

Note: Support for ASP-style tags was added in 3.0.4.

Note: 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.

The closing tag for the block will include the immediately trailing newline if one is present. Also, the closing tag automatically implies a semicolon; you do not need to have a semicolon terminating the last line of a PHP block.

PHP allows you to use structures like this:

Example 5-2. Advanced escaping

if ($expression) { 
    <strong>This is true.</strong>
} else { 
    <strong>This is false.</strong>
This works as expected, because when PHP hits the ?> closing tags, it simply starts outputting whatever it finds 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 echo() or print() or somesuch.

add a note add a note User Contributed Notes
Basic syntax
thunder at cumt dot net
26-Aug-2003 11:46
I user this for outputting my xhtml head:
echo <<<EOD
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" lang="en-US"
nd at mad dot scientist dot com
14-Feb-2003 08:07
As for the "an _SGML_ processing instruction is actually written as <?php ..>, i.ewithout a trailing question mark." issue I rather prefer <xsl:text>?</xsl:text> at the very end of the <xsl:processing-instruction> element. This nicely outputs a question mark first and then the closing bracket, XML style ("?>").

Be sure to include the <xsl:text> element, otherwise the parser may add additional whitespace after the question mark.

de \ kibo \ niels
19-Dec-2002 10:19
A follow-up to the first posting on this page: If you're writing PHP code on a non-UNIX system like MacOS (and even on a UNIX system like Mac OS X!), be very careful about line breaks.

Although this was apparently fixed in 4.0.5, I got the weirdest errors while testing PHP scripts that I wrote in BBEdit (6.5) on MacOS X (10.2, running Apache 1.3.27 with PHP 4.1.2).

The Mac-style line breaks (\r) that BBEdit usually produces by default confuse the PHP parser, and it will report the strangest parse errors. (I even got errors in lines of code that were commented out! This drove me nuts for days).

Your documents have to use UNIX-style linebreaks (\n) to prevent these errors. It's advisable to configure BBEdit to produce UNIX line breaks by default in every new document (preferences!).

This problem possibly also occurs with DOS/Win systems whose linebreaks are \r\n.
stodden at in dot tum dot de
19-Jun-2002 04:51
if you're experiencing problems with php PIs when generating  creating mixed php/html content with e.g. an XSLT processor in html output mode:
it's not the processors fault.
an _SGML_ processing instruction is actually written as <?php ..>, i.ewithout a trailing question mark.

xsl:processing-instruction name="php">
will therefor not not work like it should.

a sane solution to work around this is generating <scripttags instead.
dave at [nospam] dot netready dot biz
18-Mar-2002 08:21
A little "feature" of PHP I've discovered is that the <?PHP token requires a space after it whereas after the <? and <% tokens a space is optional.

The error message you get if you miss the space is not too helpful so be warned!

These examples only give a warning with error_reporting(E_ALL) )

PHP/*<Some HTML>*/?> fails...
<?/*<Some HTML>*/?> works...
mrtidy at mail dot com
12-Dec-2001 04:36
[Ed Note:
This is because of short_tags, <?xml turns php parsing onbecause of the <?.

I am moving my site to XHTML and I ran into trouble with the <?xml ?> interfering with the <?php ?> method of escaping for HTML.  A quick check of the mailing list confirmed that the current preferred method to cleanly output the <?xml ?> line is to echo it:<br>
<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?>

<Language ReferenceInstruction separation>
 Last updated: Thu, 21 Aug 2003
show source | credits | sitemap | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sat 01 Nov 2003 04:13:36 EST EST