PHP's syntax is borrowed primarily from C. Java and Perl have also influenced the syntax.
There are four ways of escaping from HTML and entering "PHP code mode":
Example 5-1. Ways of escaping from HTML
1. <? echo ("this is the simplest, an SGML processing instruction\n"); ?> 2. <?php echo("if you want to serve XML documents, do like this\n"); ?> 3. <script language="php"> echo ("some editors (like FrontPage) don't like processing instructions"); </script> 4. <% echo ("As of PHP 3.0.4 you may optionally use ASP-style tags"); %> <%= $variable; # This is a shortcut for "<%echo .." %>
The first way is only available if short tags have been enabled (either by calling short_tags(), they are configured on using the short_tags run-time configuration setting, or they are enabled using the --enable-short-tags compile-time configuration setting.
The fourth way is only available if ASP-style tags have been enabled using either the asp_tags configuration setting or the --enable-asp-tags compile-time configuration setting.
The closing "bracket" for the block will include the immediately trailing newline if one is present.
Instructions are separated the same as in C or perl - terminate each statement with a semicolon.
The closing tag (?>) also implies the end of the statement, so the following are equivalent:
<?php echo "This is a test"; ?> <?php echo "This is a test" ?>
PHP supports 'C', 'C++' and Unix shell-style comments. For example:
<?php echo "This is a test"; // This is a one-line c++ style comment /* This is a multi line comment yet another line of comment */ echo "This is yet another test"; echo "One Final Test"; # This is shell-style style comment ?>
The "one-line" comment styles actually only comment to the end of the line or the current block of PHP code, whichever comes first.
<h1>This is an <?# echo "simple";?> example.</h1> <p>The header above will say 'This is an example'.
You should be careful not to nest 'C' style comments, which can happen when commenting out large blocks.
<?php /* echo "This is a test"; /* This comment will cause a problem */ */ ?>