PHP code must be contained in special tags so that the PHP interpreter can identify it. Depending on the PHP configuration, these tags can take several forms:
<?php PHP CODE GOES IN HERE ?>
|This is the most commonly used (and recommended) form. It is known as the XML style, because it can be used inside of an XML document without causing the document to become poorly formed.|
<script language="php"> PHP CODE GOES IN HERE </script>
|HTML or Script style tags.|
<? PHP CODE GOES HERE ?>
|"Short" tags. Must be enabled via the short_open_tag php.ini configuration file directive.|
<% PHP CODE GOES HERE %>
|ASP-style tags. Must be enabled via the asp_tags php.ini configuration file directive.|
In this manual, we will use the first form shown as it is the most common and the most portable.
PHP statements must be inside of PHP tags to be processed by the PHP interpreter. Each PHP statement must end with a semi-colon, which tells the PHP interpreter that the statement is complete. If a semi-colon does not appear at the end of a line, the interpreter will assume that the statement continues onto the next line.
The PHP interpreter condenses all sequential whitespace in PHP scripts to a single whitespace. This convenient feature allows PHP developers to structure their code in a readable format without being concerned about the effects of line breaks and tabs.
PHP has two forms of comments:
// This is a single-line comment /* This is a multi-line comment. */
There are literally hundreds of built-in PHP functions that do everything from returning the current date and time on the server to pulling data out of a database. A function might take zero arguments (e.g,
phpinfo(), which returns information on the PHP environment) or it might take several arguments (e.g,
mail(), which takes three required and two optional arguments). The syntax for calling a function is straightforward:
The example below shows how the
phpinfo() function works.
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>PHPINFO</title> </head> <body> <?php //Output information on the PHP environment phpinfo(); ?> </body> </html>
PHP functions are well documented at http://www.php.net. You can quickly look up documentation on a function by going to http://www.php.net/function_name. For example, to see documentation on
phpinfo(), go to http://www.php.net/phpinfo.
Another very good function reference is located at http://www.phpdig.net/ref.
It is an unwritten rule that every programming course must contain a "Hello World!" script. Here it is:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>Hello World!</title> </head> <body> <?php //Write out Hello World! echo 'Hello World!'; ?> </body> </html>
Notice the following about the above code:
?>is processed by the PHP interpreter.
echocommand is used to print text back to the browser.
This code isn't very exciting. In fact, PHP doesn't buy us anything here as we could have just as easily output the result using straight HTML. There is nothing dynamic about this script. After learning about variables, we'll take a look at some more interesting examples.