The Basics of PHP Scripts
Let's jump straight in with a PHP script. To begin, open your favorite text editor. Like HTML documents, PHP files are made up of plain text. You can create them with any text editor, such as Notepad on Windows, Simple Text and BBEdit on Mac OS, or vi and Emacs on Unix operating systems. Most popular HTML editors provide at least some support for PHP.
TIP
Keith Edmunds maintains a handy list of PHP-friendly editors at http://phpeditors.linuxbackup.co.uk/.
Type in the example in Listing 3.1 and save the file, calling it something like first.php.
Listing 3.1 A Simple PHP Script
1: <?php 2: echo "Hello Web!"; 3: ?>
If you are not working directly on the machine that will be serving your PHP script, you will probably need to use an FTP client such as WS-FTP for Windows or Fetch for Mac OS to upload your saved document to the server.
Once the document is in place, you should be able to access it via your browser. If all has gone well, you should see the script's output. Figure 3.3 shows the output from the first.php script.
Figure 3.3 Success: the output from Listing 3.1.
Beginning and Ending a Block of PHP Statements
When writing PHP, you need to inform the PHP engine that you want it to execute your commands. If you don't do this, the code you write will be mistaken for HTML and will be output to the browser. You can designate your code as PHP with special tags that mark the beginning and end of PHP code blocks. Table 3.1 shows four such PHP delimiter tags.
Table 3.1 PHP Start and End Tags
Tag Style |
Start Tag |
End Tag |
Standard tags |
<?php |
?> |
Short tags |
<? |
?> |
ASP tags |
<% |
%> |
Script tags |
<SCRIPT LANGUAGE="php"> |
</SCRIPT> |
Of the tags in Table 3.1, only the standard and script tags are guaranteed to work on any configuration. The short and ASP-style tags must be explicitly enabled in your php.ini.
To activate recognition for short tags, you must make sure that the short_open_tag switch is set to On in php.ini:
short_open_tag = On;
Short tags are enabled by default, so you need to edit php.ini only if you want to disable them.
To activate recognition for the ASP-style tags, you must enable the asp_tags setting:
asp_tags = On;
After you have edited php.ini, you should be able to use any of the four styles in your scripts. This is largely a matter of preference, although if you intend to include XML in your script, you should disable the short tags (<? ?>) and work with the standard tags (<?php ?>).
CAUTION
The character sequence <? tells an XML parser to expect a processing instruction and is therefore frequently included in XML documents. If you include XML in your script and have short tags enabled, the PHP engine is likely to confuse XML processing instructions and PHP start tags. Disable short tags if you intend to incorporate XML in your document.
Let's run through some of the ways in which you can legally write the code in Listing 3.1. You can use any of the four PHP start and end tags that you have seen:
<? echo"Hello Web!"; ?> <?php echo "Hello Web!"; ?> <% echo "Hello Web!"; %> <SCRIPT LANGUAGE="php"> echo "Hello Web!"; </SCRIPT>
You can also put single lines of code in PHP on the same line as the PHP start and end tags:
<? echo "Hello Web!"; ?>
Now that you know how to define a block of PHP code, let's take a closer look at the code in Listing 3.1 itself.
The echo Statement and print() Function
Simply, the echo statement is used to output data. In most cases, anything output by echo ends up viewable in the browser. You could also have used the print() function in place of the echo statement. Using echo or print() is a matter of taste; when you look at other people's scripts, you might see either use.
Whereas echo is a statement, print() is a function. A function is a command that performs an action, usually modified in some way by data provided for it. Data sent to a function is almost always placed in parentheses after the function name. In this case, you could have sent the print() function a collection of characters, or a string. Strings must always be enclosed in quotation marks, either single or double. For example:
<? print("Hello Web!"); ?>
The only line of code in Listing 3.1 ended with a semicolon. The semicolon informs the PHP engine that you have completed a statement.
A statement represents an instruction to the PHP engine. Broadly, it is to PHP what a sentence is to written or spoken English. A sentence should usually end with a period; a statement should usually end with a semicolon. Exceptions to this include statements that enclose other statements, and statements that end a block of code. In most cases, however, failure to end a statement with a semicolon will confuse the PHP engine and result in an error.
Combining HTML and PHP
The script in Listing 3.1 is pure PHP. You can incorporate this into an HTML document by simply adding HTML outside the PHP start and end tags, as shown in Listing 3.2.
Listing 3.2 A PHP Script Incorporated into HTML
1: <html> 2: <head> 3: <title>Listing 3.2 A PHP script including HTML</title> 4: </head> 5: <body> 6: <b> 7: <?php 8: echo "hello world"; 9: ?> 10: </b> 11: </body> 12: </html>
As you can see, incorporating PHP code into a predominantly HTML document is simply a matter of typing in the code. The PHP engine ignores everything outside the PHP open and close tags. If you were to view Listing 3.2 with a browser, as shown in Figure 3.4, you would see the string hello world in bold. If you were to view the document source, as shown in Figure 3.5, the listing would look exactly like a normal HTML document.
Figure 3.4 The output of Listing 3.2 as viewed in a browser.
Figure 3.5 The output of Listing 3.2 as HTML source code.
You can include as many blocks of PHP code as you need in a single document, interspersing them with HTML as required. Although you can have multiple blocks of code in a single document, they combine to form a single script. Any variables defined in the first block will usually be available to subsequent blocks.
Adding Comments to PHP Code
Code that seems clear at the time of writing can seem like a hopeless tangle when you try to amend it six months later. Adding comments to your code as you write can save you time later on and make it easier for other programmers to work with your code.
A comment is text in a script that is ignored by the PHP engine. Comments can be used to make code more readable or to annotate a script.
Single-line comments begin with two forward slashes (//) or a single hash sign (#). The PHP engine ignores all text between these marks and either the end of the line or the PHP close tag:
// this is a comment # this is another comment
Multiline comments begin with a forward slash followed by an asterisk (/*) and end with an asterisk followed by a forward slash (*/):
/* this is a comment none of this will be parsed by the PHP engine */