PHP: Basic syntax - Manual
PHP  
downloads | documentation | faq | getting help | | php.net sites | links 
search for in the  
previous���Բο�Instruction separationnext
Last updated: Wed, 24 Jul 2002
view this page in Printer friendly version | English | Brazilian Portuguese | Czech | Dutch | Finnish | French | German | Hungarian | Italian | Japanese | Korean | Polish | Romanian | Russian | Spanish | Turkish

�� 6. 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:

���� 6-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");
    </script>

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.

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

ע: 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:

���� 6-2. Advanced escaping

<?php
if ($expression) { 
    ?>
    <strong>This is true.</strong>
    <?php 
} else { 
    ?>
    <strong>This is false.</strong>
    <?php 
}
?>
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.

User Contributed Notes
Basic syntax
add a note about notes

03-Jan-2001 08:35

[Ed Note: this was fixed in 4.0.5  [email protected]]

If you're using a MacOS text editor, such as BBEdit, for composing your
PHP, you'll want to save your files with Unix line breaks.
This fixes the "error on line 1" problem where PHP's error
system doesn't recognize the MacOS line breaks properly. It will also
allow you more flexibility in creating multi-line SQL statements.

11-Oct-2001 06:34
I use FrontPage 2000 exclusively for creating my PHP documents.  I use the
<% %> style syntax.  I can put code anywhere, before head (as is
required for cookie data) and anywhere else in between.  Using advanced
flow control, i can design an entire page in html, and then put PHP
control statements around it.  This is much easier than <% echo %>
commands.  You can drop in/out of PHP at ANY time, and the only reason to
ever use <% echo %> is for variables.  Save yourself some hassle and
write your html in html and avoid the echo mess.


12-Dec-2001 06:36

[Ed Note:
This is because of short_tags, <?xml turns php parsing on, because of
the <?.
[email protected]]

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:
<?php echo("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"); ?>


16-Jan-2002 07:40

Under php 3.0.14 :
if you have a line like this :
echo "?> written to file";
And you wish to comment this line, you'll do this :
//echo "?> written to file";
But this will generate a parse error at the end of script (even if this
line was in an include script).

Why?  because '?>' is ignored as long as it is inside "".
But once you've commented, the echo function is ignored, and '?>' takes
its signification : end of script!

I guess it was corrected on next version, but if you run under php 3.0.14
be careful, it make me loose a lot of time!

Paquerette


18-Mar-2002 10: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...


17-Jun-2002 06:50

I can't find out how to break the line in the middle of a function.  I have
tried the standard Unix '\' continuation trick, but that doesn't work.


18-Jun-2002 08: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.e. without a trailing question mark.

<xsl:processing-instruction name="php">
  echo $hello;
</xsl:processing-instruction>
will therefor not not work like it should.

a sane solution to work around this is generating <script> tags
instead.


09-Jul-2002 08:42

also, if you're using Mac OS X...i highly suggest looking into Dreamweaver
MX.  it's been a DREAM in learning and using PHP.  no problems with line
breaks.  it also colors your code based on the codes type and has pop-up
tips on what goes in certian brackets and functions.  really sharp...


15-Jul-2002 10:37

Need help how I can build a  mysql fot php to get a localhost ??

15-Jul-2002 03:42
You don't need the closing tag if you don't plan to add html (or something)
afterwards. This works (but the ; must be there):

<? phpinfo();


15-Jul-2002 06:45

Ich habe hier einen Code der mir egentlich sagen m�sste: Ihr letzter
versuch war am...
Er funktioniert aber nicht, deshalb gebe ich in hier rein, falls mir
irgendjemand diesen Code richtigstellen kann, sollte er mir bitte ein mail
schicken.
Hier der Code:

<?php
$lastvisit = $http_COOKIE_VARS
[,,lastvisit��];
if (!$lastvisit)
{
echo ,,Sie haben uns in diesen Monat noch nicht beehrt!
��; } else { echo ,,Ich letzter Besuch war am: $lastvisit��; } $datum = date(,,d.m.Y H:i:s��) setcookie(lastvisit��, $datum, time()+30*24*60*60); // 30 tage lang g�ltig ?>

add a note about notes
previous���Բο�Instruction separationnext
Last updated: Wed, 24 Jul 2002
show source | credits | stats | mirror sites:  
Copyright © 2001, 2002 The PHP Group
All rights reserved.
This mirror generously provided by:
Last updated: Sun Jul 28 20:06:57 2002 CEST