You are here

Using PHP For Interactive Web Pages

Author(s): 
Andrew G. Bennett

When I first started writing web pages, I found it frustrating that, while it was easy to create nice looking forms in plain HTML, it was much more difficult to set up my site to actually respond to (or even record) data submitted via a form. Fortunately, as the web has grown, so have the tools available to handle such tasks. Simple processing of form data is now reasonably easy and allows a web site to become much more interactive. In this article, I present an overview of PHP as one of the useful tools for adding interactivity to a web site beyond a set of hyperlinks. The name is one of those cute self-referential acronyms, the PHP Hypertext Preprocessor. While PHP is a full programming language, it is designed to make adding basic interactivity to a web page easier than it would be with Java applets or CGI programs. In particular, PHP makes it very easy to handle data submitted from a standard HTML web form. PHP code is embedded into a standard web page, where it is set off with tags. (Some variations are permitted on the exact syntax of the tags.) When a browser requests a PHP page from the server, the server scans the contents of the page, then runs the code in the tags and replaces the tags and their content with the output of the code before sending the page to the browser. Since the code is embedded directly into the page, PHP is fairly quick to write and easy to maintain. PHP is not the only language to use such a technique -- Microsoft's Active Server Pages and server-side JavaScript both work similarly. PHP is the open-source variation on this common theme.

Andrew Bennett is Professor of Mathematics at Kansas State University.

Despite being based on C syntax, PHP is fairly easy to use. Part of the ease of use is because it is an interpreted language, i.e. you just type the code directly into the web page. You don't need to provide any headers or other control codes for your program -- you can include just a single line of code if you want. So, while PHP provides a full programming language, you can use as little of the language as you want. In addition, you update your program just by updating your web page. Since the output of PHP is just standard HTML, PHP works with any browser without any plug-ins. PHP provides easy hooks to access other programming languages, including the GD library for images and numerous database systems. You can also link to general processes running on the server -- though doing so can open security holes.

A PHP page can also include JavaScript code and applet tags to include Java applets. However, communication between the different languages is problematic because PHP code is executed by the server before the page is sent to the browser, while (client-side) JavaScript and Java are run by the browser. The PHP code can output JavaScript function calls or Java PARAM tags, so PHP can send results to JavaScript or Java. On the other hand, to send results from Java applets or JavaScript functions to PHP code, it is necessary to put the results into GET or POST variables and send them to the server with a request to (re-)load a web page. 

Published June, 2002
© 2002 by Andrew G. Bennett

 

Andrew G. Bennett, "Using PHP For Interactive Web Pages," Convergence (December 2004)