<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <HTML ><HEAD ><TITLE >Basic syntax</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="Manual PHP" HREF="index.html"><LINK REL="UP" TITLE="Documentaþia limbajului" HREF="langref.html"><LINK REL="PREVIOUS" TITLE="Documentaþia limbajului" 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-2"></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" >Manual PHP</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="langref.html" ACCESSKEY="P" >Înapoi</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" >Înainte</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="chapter" ><H1 ><A NAME="language.basic-syntax" >Cap. 5. Basic syntax</A ></H1 ><DIV CLASS="TOC" ><DL ><DT ><B >Cuprins</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" >Escaping from HTML</A ></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="AEN526" ></A ><P ><B >Exemplu 5-1. Ways of escaping from HTML</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> 1. <font color="#0000BB"><?php </font><font color="#007700">echo(</font><font color="#DD0000">"if you want to serve XHTML or XML documents, do like this\n"</font><font color="#007700">); </font><font color="#0000BB">?><br /></font><br />2. <font color="#0000BB"><? </font><font color="#007700">echo (</font><font color="#DD0000">"this is the simplest, an SGML processing instruction\n"</font><font color="#007700">); </font><font color="#0000BB">?><br /></font> <font color="#0000BB"><?= expression ?></font> This is a shortcut for "<font color="#0000BB"><? </font><font color="#007700">echo </font><font color="#0000BB">expression ?></font>"<br /> <br />3. <font color="#0000BB"><script language="php"><br /> </font><font color="#007700">echo (</font><font color="#DD0000">"some editors (like FrontPage) don't<br /> like processing instructions"</font><font color="#007700">);<br /> </font><font color="#0000BB"></script><br /></font><br />4. <% echo ("You may optionally use ASP-style tags"); %><br /> <%= $variable; # This is a shortcut for "<% echo . . ." %></font> </code></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="ini.sect.language-options.html#ini.short-open-tag" >short_open_tag</A > configuration setting in the PHP config file, or by compiling PHP with the <VAR CLASS="option" >--enable-short-tags</VAR > option to <B CLASS="command" >configure</B >. Even if it is enabled by default in <TT CLASS="filename" >php.ini-dist</TT >, use of short tags are discouraged. </P ><P > The third way is always available and safe like the first one. However, the first is the preferred and most used one. </P ><P > The fourth way is only available if <SPAN CLASS="productname" >ASP</SPAN > tags have been enabled using the <A HREF="ini.sect.language-options.html#ini.asp-tags" >asp_tags</A > configuration setting. <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </B >Support for <SPAN CLASS="productname" >ASP</SPAN > tags was added in 3.0.4.</P ></BLOCKQUOTE ></DIV > </P ><P > <DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >Notã: </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 ><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. Closing tag of a PHP block at the end of a file is optional. </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="AEN548" ></A ><P ><B >Exemplu 5-2. Advanced escaping</B ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /></font><font color="#007700">if (</font><font color="#0000BB">$expression</font><font color="#007700">) { <br /> </font><font color="#0000BB">?><br /></font> <strong>This is true.</strong><br /> <font color="#0000BB"><?php <br /></font><font color="#007700">} else { <br /> </font><font color="#0000BB">?><br /></font> <strong>This is false.</strong><br /> <font color="#0000BB"><?php <br /></font><font color="#007700">}<br /></font><font color="#0000BB">?></font> </font> </code></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" >Înapoi</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Acasã</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="language.basic-syntax.instruction-separation.html" ACCESSKEY="N" >Înainte</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >Documentaþia limbajului</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="langref.html" ACCESSKEY="U" >Sus</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >Instruction separation</TD ></TR ></TABLE ></DIV ></BODY ></HTML >