Code for a PHP Times Table

 

The code for the Times Table in the previous page uses a For Loop. The Start for the loop will come from the Start Number textbox, and the end of the loop will come from the End Number textbox. Here's the code in full (without the HTML):

<?PHP

$times = 2;

if (isset($_POST['Submit1'])) {

$start = $_POST['txtStart'];
$end = $_POST['txtEnd'];
$times = $_POST['txtTimes'];

for($start; $start <= $end; $start++) {

$answer = $start * $times;
print $start . " multiplied by " . $times . " = " . $answer . "<BR>";

}

}

?>


Code Explanation

We need all those numbers from the textboxes on the form, so we start with:

$times = 2;

if (isset($_POST['Submit1'])) {

$start = $_POST['txtStart'];
$end = $_POST['txtEnd'];
$times = $_POST['txtTimes'];

}

The first line just puts a value in the variable called $times . This is so that the "Multiply By" textbox will have a default value when the page is loaded.

Next we use the isset( ) function again, just to check if the user clicked the Submit button. This is exactly the same as you saw in the last section.

To get the values from the textboxes, we use the following:

$start = $_POST['txtStart'];
$end = $_POST['txtEnd'];
$times = $_POST['txtTimes'];

Again, this is code you met in the last section. You just assign the values from the textboxes to the new variables using $_POST[]. In between the square brackets, we've typed the NAME of the HTML textboxes. So this gives us the values that the user entered on the form. Next comes out For Loop:

for($start; $start <= $end; $start++) {

$answer = $start * $times;

}

Let's look at that first line again:

for($start; $start <= $end; $start++) {

So we have a starting value for our loop, an end value, and an update expression. The starting value is coming from the variable called $start. This will be whatever number the user entered in the first textbox. The default is 1. Look at the end value, though:

$start <= $end

The end value is when the value in the variable called $start is less than or equal to the value held in the variable called $end. This works because we're increasing the value of $start each time round the loop. The variable called $end is a fixed value, and comes from the textbox on the form.

The last part of the loop code is the update expression. This tells PHP to increase the value of $start each time round the loop:

$start++

The double plus symbol (++) means "add 1 to the number held in $start".

And that's the essence of for loops: provide a start value, an end value, and how you want to update each time round the loop.

The code inside the for loop, however, the code that gets executed each time round the loop, is this:

$answer = $start * $times;

Remember, the variable $times holds the times table, the 2 times table by default. This is being multiplied by whatever is inside the variable $start. Each time round the loop, $start will have a different value – first 1, then 2, then 3, etc. The answer is then stored in the variable that we called $answer. So it's really doing this:

$answer = 1 * 2;
$answer = 2 * 2;
$answer = 3 * 2;
etc

Finally, we displayed the result to the page like this:

print $start . " multiplied by " . $times . " = " . $answer . "<BR>";

This is just concatenation. See if you can work out what all the parts do!

And that’s it – your very own times table generator. If you have children, show them the programme you wrote. They’ll be very impressed and tell you how brilliant you are. Children are like that.

Of course, your programme is not perfect, which I’m sure the children will discover. Especially if they enter a 10 as the start number and a 1 as the end number. Why doesn't it print anything out? Anything you can do to trap this error? Another if statement somewhere, perhaps?