<HTML ><HEAD ><TITLE >Basic syntax</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Language Reference" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Language Reference" HREF="langref.html"><LINK REL="NEXT" TITLE="Instruction separation" HREF="language.basic-syntax.instruction-separation.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="chapter" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="langref.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="language.basic-syntax.instruction-separation.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.basic-syntax" >Chapter 6. Basic syntax</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Table of Contents</B ></DT ><DT ><A HREF="language.basic-syntax.html#language.basic-syntax.phpmode" >Escaping from HTML</A ></DT ><DT ><A HREF="language.basic-syntax.instruction-separation.html" >Instruction separation</A ></DT ><DT ><A HREF="language.basic-syntax.comments.html" >Comments</A ></DT ></DL ></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="language.basic-syntax.phpmode" ></A >Escaping from HTML</H1 ><P > 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. </P ><P > 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 <TT CLASS="filename" >php.ini</TT > 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. </P ><P > The tags supported by PHP are: </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3123" ></A ><P ><B >Example 6-1. Ways of escaping from HTML</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >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"); </script> 4. <% echo ("You may optionally use ASP-style tags"); %> <%= $variable; # This is a shortcut for "<% echo . . ." %></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > The first way, <?php. . .?>, is the preferred method, as it allows the use of PHP in XML-conformant code such as XHTML. </P ><P > The second way is not available always. Short tags are available only when they have been enabled. This can be done via the <B CLASS="function" >short_tags()</B > function (PHP 3 only), by enabling the <A HREF="configuration.directives.html#ini.short-open-tag" >short_open_tag</A > configuration setting in the PHP config file, or by compiling PHP with the --enable-short-tags option to <B CLASS="command" >configure</B >. Even if it is enabled by default in php.ini-dist, use of short tags are discouraged. </P ><P > The fourth way is only available if ASP-style tags have been enabled using the <A HREF="configuration.directives.html#ini.asp-tags" >asp_tags</A > configuration setting. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Note: </B >Support for ASP-style tags was added in 3.0.4.</P ></BLOCKQUOTE ></DIV > </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >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. </P ></BLOCKQUOTE ></DIV ><P > 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. </P ><P > PHP allows you to use structures like this: <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN3139" ></A ><P ><B >Example 6-2. Advanced escaping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" ><?php if ($expression) { ?> <strong>This is true.</strong> <?php } else { ?> <strong>This is false.</strong> <?php } ?></PRE ></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > 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 <A HREF="function.echo.html" ><B CLASS="function" >echo()</B ></A > or <A HREF="function.print.html" ><B CLASS="function" >print()</B ></A > or somesuch. </P ></DIV ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.basic-syntax.instruction-separation.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Language Reference</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Instruction separation</TD ></TR ></TABLE ></DIV ></BODY ></HTML >