Getting into PHP
Last week we started looking at the Web server scripting language PHP. As we saw, the open source language is not only wildly popular, but also supported by a range of commercial tools. Add to that a great support site and a tremendous library of documentation and you can see why PHP is so highly regarded.
So PHP looks good, but what is the language really about?
Well, remember that PHP is a scripting language so its role is to augment the functionality of Web servers when it comes to dynamically handling content. The PHP interpreter runs as a Common Gateway Interface application or, on Microsoft's IIS, as a CGI application or an ISAPI process.
To begin with, the PHP interpreter must be told which documents to handle - all documents, just those from certain subdirectories or only those with the extension .php. The document content could be HTML with embedded PHP scripts or only a PHP script.
Now the PHP interpreter needs to find the PHP scripts in the document. One method is to use "standard tags" ("<?php … ?>") in an HTML document like this:
<html>
<head><title>Gearhead</title>
</head>
<body><p><?php print time(); ?></p></body>
</html>
Note that we could have written the PHP script on multiple lines and that a semicolon indicates the end of a line:
<body><p>
<?php
print time();
?>
</p></body>
Before we look at other ways to tag PHP scripts, let's explain what this document is meant to do. When a Web browser requests this document from a PHP-enabled Web server, the PHP interpreter scans the content for PHP tags. When a PHP tag is found, the script is read and interpreted. The output of the script, usually a stream of text, dynamically replaces the script in the document (the original document still exists).
In the script above, the PHP time function returns the number of seconds since midnight on Jan. 1, 1970 (this date and time is called the Unix epoch). Thus, the above document is changed on the fly, and the time value (shown below as 987654321) replaces the script:
<html>
<head><title>Gearhead</title>
</head>
<body><p>987654321</p></body>
</html>
A document can contain multiple scripts, and the scripts can access common variables and functions. There are also other ways of tagging PHP scripts. A more explicit form is "script tags:"
<SCRIPT LANGUAGE="php"> print time(); </SCRIPT>
If the Web server is set up to handle Web pages with Microsoft Active Server Page, then the above script could be embedded in Active Server Page tags: <% print time(); %>
Finally, "short tags" can be used: <? print time(); ?>
Note that short tags will cause a problem if you use documents containing XML - standard tags are preferred.
Next week, we'll roll up our sleeves and get a little deeper into PHP.
RELATED LINKS
Comments and suggestions to gh@gibbs.com.
Gibbs Forum
The place to discuss Gibbs's columns.
Check out this week's edition of
Backspin for more musings from Gibbs.
