For PHP to work in a file, it should end with the extension, .php. It is possible to configure your server to run on files with different extension but that is beyond the scope of this tutorial.
Blocks of PHP code is enclosed by opening and closing tags. There are four possible sets of tags you can use:
| Tag style | Opening tag | Closing tag |
|---|---|---|
| Standard Tags | <?php | ?> |
| Short Tags | <? | ?> |
| ASP Tags | <% | %> |
| Script Tags | <script language="php"> | </script> |
The officially supported syntax is <php ?> so it is best to use that but I use the short tags.
To keep your code understandable when you come back to it six months after you first write it and for other people who may use it, you can use comments. Comments are lines of text that are ignored by the PHP engine. There are two ways to make a single line comment:
<?
// this a comment
# and this a comment
?>
The text is ignored from the // or # till the end of the line. If you want to make a comment that takes several lines, you make it like this:
<?
/*
This is a multi-line comment.
The whole block will be ignored by the PHP engine.
If you are making a file to be copied by other people, it is a good idea to use multi-line comments to prevent errors.
*/
?>
In my opinion, comment tags are the most important tags in PHP. Not only are they useful to you for making your code understandable when you come back to it six months after you first write it, they are invaluable for debugging. You often have only a general idea of where the bug is and you can narrow it down by commenting out single lines or blocks of code. If you are making code code that other people will be using it is a good idea to have a block of comments at the top of the script explaining what the script does, and what the user may need to change.
Now that you know how to turn PHP on and off, lets look at an actual php script. [Next]
|
|
|