Every programming language has a number of methods for creating loops, and PHP is no exception. Try this program, for example, either by uploading it the server and seeing what happens, or just by reading the code and trying to work it out:
<?php
echo "<HTML>";
echo "<body>";
echo "A web server that knows its 7 times table!<br><br>";
for ($x = 1; $x <= 10; $x++)
{
echo $x . " times 7 is " . $x * 7 . "<br>";
}
echo "</body>";
echo "</HTML>";
?>
Run the program, and you'll see the following in your web browser:
One line of PHP that you'll not have seen before is:
for ($x = 1; $x <= 10; $x++)
This is one of PHP's methods of creating a loop. In this case, it's a "for" loop. If you ever learnt to program in Basic many years ago, it's just the same as a FOR…NEXT loop in that language.
Translating the line of PHP into English, the line of code means: set variable $x to 1, and execute everything within the curly brackets that follow. If $x is less than, or equal, to 10, exit the loop and jump to the first line of code after the section in curly brackets. Otherwise, add 1 to $x and go round again.
The $x++ is a PHP shorthand way of saying "add 1 to $x". Another way, which works just as well and which is more familiar to Basic programmers, is $x = $x + 1.
By changing the starting value (1) and the finishing value (10), you can adapt this code to run as many times as you like. Try it.
As for generating our 7 times table, the line that does all the work is:
echo $x . " times 7 is " . $x * 7 . "<br>";
Here, we’ve used a single echo statement to produce the entire line of our table. Each part of the line is separated by a dot. Literal text such as times 7 is gets enclosed in quote marks, whereas we leave the quote marks out if we want PHP to echo the value of a variable such as $x, or make a calculation such as $x * 7.
Don't forget that, whenever you write a PHP program, you're creating a program that will generate a web page. This needs to be a valid web page, complete with <HTML> and <body> tags. Otherwise, the visitor's web browser may get confused.
Another type of loop that's used extensively in PHP, especially when accessing MySQL databases, is the "while" loop. Here's the 7 times table again, this time using a "while" loop:
<HTML>
<body>
My web server still knows its 7 times table
<br><br>
<?php
$i=1;
while($i <= 10)
{
echo $i;
echo " times 7 is ";
$product = $i * 7;
echo $product . "<br>";
$i = $i + 1;
}
?>
</body>
</HTML>
Just to be different, I've mixed HTML and PHP modes this time.
Notice how a "while" loop works. First, we set $i to 1. Then we create the loop, which says "do the block of code within the curly brackets, but only while $i is less than, or equal to, 10".
Because there's a line within the block which adds 1 to $i, there will come a point at which $i is not less than or equal to 10, so the program will fall through to the next line after the closing curly bracket.
<?php
echo "<HTML>";
echo "<body>";
echo "A web server that knows its 7 times table!<br><br>";
for ($x = 1; $x <= 10; $x++)
{
echo $x . " times 7 is " . $x * 7 . "<br>";
}
echo "</body>";
echo "</HTML>";
?>
Run the program, and you'll see the following in your web browser:
One line of PHP that you'll not have seen before is:
for ($x = 1; $x <= 10; $x++)
This is one of PHP's methods of creating a loop. In this case, it's a "for" loop. If you ever learnt to program in Basic many years ago, it's just the same as a FOR…NEXT loop in that language.
Translating the line of PHP into English, the line of code means: set variable $x to 1, and execute everything within the curly brackets that follow. If $x is less than, or equal, to 10, exit the loop and jump to the first line of code after the section in curly brackets. Otherwise, add 1 to $x and go round again.
The $x++ is a PHP shorthand way of saying "add 1 to $x". Another way, which works just as well and which is more familiar to Basic programmers, is $x = $x + 1.
By changing the starting value (1) and the finishing value (10), you can adapt this code to run as many times as you like. Try it.
As for generating our 7 times table, the line that does all the work is:
echo $x . " times 7 is " . $x * 7 . "<br>";
Here, we’ve used a single echo statement to produce the entire line of our table. Each part of the line is separated by a dot. Literal text such as times 7 is gets enclosed in quote marks, whereas we leave the quote marks out if we want PHP to echo the value of a variable such as $x, or make a calculation such as $x * 7.
Don't forget that, whenever you write a PHP program, you're creating a program that will generate a web page. This needs to be a valid web page, complete with <HTML> and <body> tags. Otherwise, the visitor's web browser may get confused.
Another type of loop that's used extensively in PHP, especially when accessing MySQL databases, is the "while" loop. Here's the 7 times table again, this time using a "while" loop:
<HTML>
<body>
My web server still knows its 7 times table
<br><br>
<?php
$i=1;
while($i <= 10)
{
echo $i;
echo " times 7 is ";
$product = $i * 7;
echo $product . "<br>";
$i = $i + 1;
}
?>
</body>
</HTML>
Just to be different, I've mixed HTML and PHP modes this time.
Notice how a "while" loop works. First, we set $i to 1. Then we create the loop, which says "do the block of code within the curly brackets, but only while $i is less than, or equal to, 10".
Because there's a line within the block which adds 1 to $i, there will come a point at which $i is not less than or equal to 10, so the program will fall through to the next line after the closing curly bracket.