A Simple PHP Program to Do Iteration

by D.W. Hyatt

This set of notes has two programs. One creates a form that gathers data and sends it to another program to process. Both programs use PHP, an addition to HTML, so therefore the endings of the file names reflect that fact. Commands that are part of PHP will be found between the two markers:

<?     Starting PHP

PHP Code

?>     Ending PHP


DATA COLLECTION PROGRAM: iter.php3

This program asks the user to enter a single number, which it will then send to another program to display the first 10 iterations of that value used in a function. The PHP code does nothing more than print the current date, but that's cool.

The FORM portion is entered as TEXT, and the "NAME" is actually a variable called data that the second program will use to learn what value was entered by the user.

<HTML>
<BODY BGCOLOR = "#FFFFFF">
<CENTER>
<H1> Iteration Program </H1>
</CENTER>
Today's Date: 

<?
  print(date("l F d, Y"));
?>

<H3> Enter a Value to Iterate </H3>

<FORM ACTION = "results.php3" METHOD = POST>

<INPUT TYPE="text" NAME = "data">
<P>
<INPUT TYPE="submit" VALUE = "Show Results">

</BODY>
</HTML>



PROCESSING PROGRAM: results.php3

This program receives the numerical value from the calling page (iter.php3), and uses that number in an iterative loop as it calculates successive squares of the number. The number sent from the above form program is stored in a variable called $data. All variables in PHP are preceeded by the dollar sign. The program uses a simple "for loop" to calculate the square of the number entered.
<HTML>
<BODY BGCOLOR = "#FFFFFF">
<CENTER>
<H1> Iteration Results </H1>
</CENTER>
<B>Here are 10 iterations of the formula: <BR>
    
y = x <SUP> 2 </SUP>
</B>
<P>

<!-- PHP Calculations start here!  -->

<?
  $num = $data;
  print("Original value of x:   <B>$data </B> <P>");
  for($i = 1; $i <= 10; $i++)
   {
    $num = $num * $num;
    print("$i. $num <BR>");
   }
?>

</BODY>
</HTML>

Run the Iteration Program

iter.php3


Assignment

  1. Write a program that will demonstrate the logistic difference equation that we have discussed in class. This equation was used in the Ecology discussion in your text, Chaos, Fractals, and Dynamics in Chapter 1 as well as in Chapter 2. The program we are demonstrating is program 2.7 in that text.

    This program should request information such as the initial value of x as well as the rate constant, k, and possibly the number of iterations desired. A display will be generated that shows what happens as the equation, y = k*x*(1-x) is iterated, where 0 < x < 1 and 1 <= k <= 4.

    Investigate different values of k to see if you can determine the exact point when the first Bifurcation happens. Can you find the second?


  2. Use a simple FORM option selection so that the PHP program will select from one of four different functions of your choice that can be investigated by iteration. Use your textbook to get some equations that might be interesting.