This appendix is by no means a copy of the PHP Manual (found at http://www.php.net/manual/), which is absolutely huge and contains user-submitted comments and code samples. Instead, this appendix serves as an "essential" reference—it contains the elements of PHP which (in my opinion) you can't live without. The PHP Development Team and all of the documentation contributors have done a wonderful job with the entire PHP Manual, and there's no need to reinvent the wheel. However, since this appendix touches on only a small percentage of all there is to know about PHP, check the PHP Manual before asking a question on one of the PHP mailing lists.
To combine PHP code with HTML, the PHP code must be escaped, or set apart, from the HTML. The PHP engine will consider anything within the tag pairs shown in Table A.1 PHP code.
Here's an example that uses all three in the same script, called tagtest.php:
<HTML> <HEAD> <TITLE>Tag Test</TITLE> </HEAD> <BODY> <?php echo "I am using the first tag pair type.<br>"; ?> <? echo "I am using the second tag pair type.<br>"; ?> <script language="php"> echo "I am using the third tag pair type.<br>"; </script> </BODY> </HTML>
When accessed, the script displays this in your browser:
I am using the first tag pair type.
I am using the second tag pair type.
I am using the third tag pair type.
When you create PHP scripts, you're creating a series of instructions that are sent to the PHP engine. Each instruction must end with a semicolon (;), also known as an instruction separator.
These are examples of properly terminated instructions:
<?php echo "<P>Hello World! I'm using PHP!</P>\n"; echo "<P>This is another message.</P>"; ?>
One of these instructions is missing a semicolon:
<?php echo "<P>Hello World! I'm using PHP!</p>\n" echo "<P>This is another message.</p>"; ?>
so it will produce a nasty error, such as this:
Parse error: parse error, expecting "," or ";" in /path/to/script on line 9
The last important bit of PHP syntax is the method of commenting inside code. Use double forward slashes (//) to indicate a comment:
<?php // The next statement prints a line of text echo "<P>Hello World! I'm using PHP!</P>\n" ?>
The comment "The next statement prints a line of text" is visible in your source code but is not printed in your HTML output.
For multi-line comments, you can surround your text with /* and */, like this:
<?php /* This is a really long comment that will run onto multiple lines */ ?>
Commenting code is a good habit to have, because it helps other programmers figure out what you're trying to do with a piece of code if they have to make changes or if they're trying to learn from your work.