Originally published in the 3000 NewsWire
PHP is an open-source, server-side, cross-platform scripting language that is used to generate dynamic Web pages or to build browser-based applications. The PHP script can be embedded in the HTML page to generate HTML code that is inserted in the Web page at that point, or the PHP script can generate the entire HTML output. One of PHP's strengths is easy database integration. Another is that it runs on many platforms, including Windows, LINUX and HP-UX, and even somewhat on MPE/iX: a Web page at invent3k.external.hp.com/~MGR.FETHERS describes an in-progress port by Campbell Fethers and Troy Jendra, including a TurboIMAGE database interface.
The Daily News Page (www.news.ai/daily.php3) is updated whenever there is some news about the Caribbean island of Anguilla. Some days there is no news, but many site visitors refresh the page during the day to see if there is anything new. Problem: How to keep these frequent visitors happy? Solution: generate dynamic content from the archives that varies on every refresh.
This Web page selects a random picture from a list of over 1,000 pictures, plus a link to a Web page that explains the picture. After error checking the picture filename (Does it exist? Is it readable? Is it too small or too big?), the PHP script displays the picture resized to a standard size, with a link to "zoom" to the full-size image. Then it adds the date of the picture and a link to the accompanying story.
Here is an edited extract from the daily.php3 script to show you some PHP coding:
<?php $pixdb = file('/home/anguillanews/pixdb.txt'); $count=count($pixdb); // seed and pick a random number between 0 and $i: srand((double)microtime()*1000000); $index = rand()%$count; $pixentry = explode(",",trim($pixdb[$index])); $filename = trim($pixentry[0]); $link =trim($pixentry[1]); /*story about this pix */ if ( (!file_exists($link)) || (!is_readable($link)) ) { $link = ""; } if ( (!file_exists($filename)) || (!is_readable($filename)) ) { $filename = ""; } if ($filename == "") { error_log("News/bad line in pixdb: $index", 1,"bgreen@robelle.com",""); ... ?>
This code is easy to follow: variables start with $ and values can be passed in via the URL if desired. Statements end with a semicolon, statement blocks are enclosed in braces, comments are enclosed in /* and */, and there are many built-in functions, like 'rand' and 'file' and 'count'. The script opens a file containing a list of paired images and Web pages, computes a random location in that file, reads the location, extracts the image file name and the link URL. Then it does some error checking on the data, and if it doesn't find a valid pair, it logs an error, which ends up being sent to me as an e-mail. Finally the script continues to generate the HTML to display the picture and the link (this is not shown above).
If you are new to PHP and want to get some idea of how it works, try the introductory tutorial at the PHP home page (www.php.net/tut.php). You can also check out the online manual (www.php.net/docs.php). I found the "Annotated User Manual (www.php.net/manual) very helpful. It is a Web-based document that allows you to append questions and clarifications to each page. The combined knowledge of the PHP community helps clear up a lot of special cases that can be confusing.
If you want to experiment with PHP, you can install it on most Windows or UNIX systems, or open an account at a Web hosting service (like www.pair.com).
Another place where PHP is commonly used is in database access: retrieval, display, and update. PHP has convenient built-in functions for Oracle, MS SQL Server, ODBC, dBase, mySQL, PostgreSQL, FrontBase, filePro, Informix, and InterBase. Troy Jendra has created a rough TurboIMAGE interface for PHP.
We wanted to make an archive of our customer records, and selected mySQL because it was free, very fast for retrievals, reliable, and good enough for a read-only archive. To search and display the data we used the Xitami Web server and PHP scripts (we found many good sample scripts on the Internet to get us started). The whole project was done on a Windows PC. To read about our conversion from TurboIMAGE to mySQL, visit www.robelle.com/tips/broaden.html#mysql
Below is the search Web page form; it prompts for an account number and a company name and passes them to the search.php script:
<b>Search the Database:</b> <p> <FORM METHOD="post" ACTION="search.php"> Account Number: <INPUT TYPE=text MAXLENGTH=4 NAME="account_no" SIZE=20><Br> Company Name: <INPUT TYPE=text MAXLENGTH=60 NAME="company_name" SIZE=20><br> <INPUT TYPE=submit VALUE="Search"> </FORM>
And here is part of the search.php script, showing how it takes the $account_no and $company_name fields from the Web search form (all form fields automatically become variables in the PHP script) and turns them into a database query:
<? mysql_connect("localhost", "username", "passwd") or die ("Unable to connect to server."); mysql_select_db("kco") or die ("Unable to select database."); $andor = 'and'; if(empty($account_no)) { $andor = 'or'; $account_no = '%'; if(empty($company_name)) { print"No Search Keywords Specified"; exit; } } else { if(empty($company_name)) { $company_name = '%'; } } $query = "SELECT * FROM m_customer where account_no='$account_no' $andor company_name like '%$company_name%'"; $result = mysql_query ($query) or die ("Query Failed"); $num_of_rows = mysql_num_rows ($result);
As you can see, the PHP code is relatively easy to understand. When you combine that with wide availability, fast execution, and good tutorial resources, this is a language that is worth learning, and one that is worth porting successfully to MPE/iX. In October 2001, Campbell Feathers commented on the state of the PHP/iX project:
"PHP/iX hasn't progressed significantly since I posted the page on invent3k, due to lack of time, etc. The changeover of invent3k to GCC 3 broke the build of the MPE module. As mentioned on the Web page, we successfully converted one typical complex Speedware report to a PHP/iX script, thus proving the concept. In the meantime, we are running PHP on a Linux box with Apache. Troy compiled the Java module into PHP, and wrote some Java objects to interface between PHP and JDBC access to ImageSQL. Response times have been pleasing. This has enabled Web development to proceed, and this system is already being used for production. Future conversion of these Web pages to PHP/iX, while it won't be a simple search and replace, is still expected to be reasonably straightforward.
"A CGI executable of PHP/iX is available for download from invent3k.external.hp.com/~MGR.FETHERS/download.html. I still intend to finish PHP/iX because I think that IMAGE and PHP are such a neat fit. The only question is when?"
Here are some resources to help in learning about PHP:
At phpbuilder.com there are PHP script examples, and www.alt-php-faq.org has Frequently Asked Questions. www.webmasterbase.com/printTemplate.php?aid=228 shows how to build an application with PHP and mySQL. At hotscripts.com/PHP there's lots of PHP resources, including sample scripts.