An Introduction to PHP3
Listing 1 is two examples of PHP code to show some of its features. The first page accepts two numbers to add a first and last name. The passing of the form variables to be used in the next page happens automatically, which is one of the neatest things about PHP. This doesn't check their type—that they are actually numbers—in order to give an error. It does show the way PHP can be used to send and receive information to the user.
Listing 2 is an example that generates CGI variables on the fly, and the receiving page (Listing 3) deals with any incoming variables generated. If you run this in a PHP-enabled server, you'll see the variables passed in the cgi-call in the location line of the browser. All Listing 3 does is echo the names of the variables, but it demonstrates a technique that would enable a web site to generate a list on the fly and allow the user to make selections—like a shopping cart.
Now let's connect to a MySQL database. The following code would connect as root to a database named “stores” on the local host, with password “tiger”. It executes a query against a parts table, counts the number of rows in the query to check for the actual data, and if it exists, the result is displayed as an HTML table with check boxes to select items. Listing 4 is the PHP code preceded by the structure of the table. Note that knowing the number of rows is handy for determining whether to display the query at all (if there is any information) as well as counting down until no data is left. Each row of data is a row in an HTML table, with the fields walked through by number. The array containing the row from the query can be addressed by number or by field name, so you could also execute:
This can be advantageous during development when field positions in the table or query may be changing more often than names.
The next page of HTML (called in the FORM command) will receive the checked-off selections as CGI variables, which can be retrieved even though their names are unknown, using the technique shown above. It will then insert into another table the items that have been checked off. See Listing 5.
The latest information on installing PHP is available at http://www.php.net/ or in the distribution itself. I have installed it for FreeBSD, Linux and Win32 using their supplied packages. I have installed it only for Apache, although it is supposed to work with other web servers. It is supplied as source code for platforms other than Win32. The installation for Apache requires recompiling Apache to add it in as a module, although if that is not an option, it can be installed as a binary and scripts can be run as cgi-bin scripts.
I've found PHP3 to be a very enjoyable and versatile language for web applications. As of this writing, the next version—which is primarily billed as a performance improvement—is in beta testing. (PHP 4.0 includes the ZEND engine; see their web site for details.)
John Holland programs intranet applications as a consultant for Bell Atlantic. He lives outside Washington DC with his wife and children. He can be reached at email@example.com.
|diff -u: What's New in Kernel Development||Sep 04, 2015|
|Android Candy: Copay—the Next-Generation Bitcoin Wallet||Sep 03, 2015|
|The True Internet of Things||Sep 02, 2015|
|September 2015 Issue of Linux Journal: HOW-TOs||Sep 01, 2015|
|September 2015 Video Preview||Sep 01, 2015|
|Using tshark to Watch and Inspect Network Traffic||Aug 31, 2015|
- diff -u: What's New in Kernel Development
- Using tshark to Watch and Inspect Network Traffic
- The True Internet of Things
- Problems with Ubuntu's Software Center and How Canonical Plans to Fix Them
- Concerning Containers' Connections: on Docker Networking
- September 2015 Issue of Linux Journal: HOW-TOs
- Firefox Security Exploit Targets Linux Users and Web Developers
- Android Candy: Copay—the Next-Generation Bitcoin Wallet
- Where's That Pesky Hidden Word?
- A Project to Guarantee Better Security for Open-Source Projects