You are here

Using PHP For Interactive Web Pages - A More Complicated Example

Author(s): 
Andrew G. Bennett

 Having given one example of a very simple PHP page, I'd now like to show the power of PHP with a much more complicated example. This example is extracted from an online homework system for Trigonometry. The question page randomly generates a problem of finding the length of a circular arc, given the central angle and the area of the associated sector. It also includes a scale illustration of the situation created on-the-fly by PHP. After the student submits an answer, the answer page checks the answer and either marks it correct or, if incorrect, gives the correct answer and a link to a solution page that shows how to solve the problem in detail, including references to specific pages in the text. In the full online system, each student logs in and the pseudorandom number generator is seeded uniquely for each attempt by that student. The results are written to a database, with some care taken for the security of the system. In this open example, the pseudorandom number generator is seeded by the system clock. (Links open in a new window.)

You may find it instructive to compare the source code in the links above with the page source displayed by your browser, to see the output of the PHP code, which replaces the material in the brackets. In particular, note that none of the database code is visible in the page source displayed by your browser, since the database calls didn't produce any output. In this way, database information that the program needs (including passwords) aren't visible to the end user.

Working with string variables is a little different in PHP from many other languages. In particular, in the line (from question.php)

       echo "An arc in a circle subtends a central angle of $deg$dgsym ";

produces the output

       An arc in a circle subtends a central angle of 91°

where the "$deg$dgsym" is automatically replaced by the value of the variables $deg and $dgsym inside the string. No concatenation operator is required. Of course, a concatenation operator may be needed when there is no space or special character between a string variable and a constant string. This automatic conversion of string variables simplifies many situations where you want to output string values to the web page.

In picture.php, there is an example of the creation of images in PHP. PHP links to the GD library to create Portable Network Graphics, which show on modern browsers and avoid the patent issues associated with GIFs. You call a PHP image with the same <img> tag as a regular image. consider the following lines from question.php:

          echo"<td><imgsrc=picture.php?t=$deg$dgsym&a=$a" . "cm&s=?cm ";
          echo "height=200 width=220 align=right>\n";

(Note the use of the concatenation operator . since the variable $a is followed by the string "cm" without any spaces). The output of these lines is

           <td><imgsrc=picture.php?t=91º&a=46.1cm&s=?+cm height=200 align=right>

where the values for t and a are randomly generated each time the page is loaded. This <img> tag produces the image

The ability to pass GET variables (the values after the first "?" in the src value) allows the main PHP page to create dynamically an image to display during the processing of the code on the page. I have found this one of the most useful aspects of working with PHP.

Andrew G. Bennett, "Using PHP For Interactive Web Pages - A More Complicated Example," Convergence (December 2004)