For Loops in PHP
So whats a loop then? A loop is something that goes round and
round. If I told you to move a finger around in a loop, youd have
no problem with the order (unless you have no fingers!) In programming,
its exactly the same. Except a programming loop will go round
and round until you tell it to stop. You also need to tell the programme
two other things - where to start your loop, and what to do after its
finished one lap (known as the update expression).
You can programme without using loops. But its an awful lot easier
with them. Consider this.
You want to add up the numbers 1 to 4: 1 + 2 + 3 + 4. You could do
it like this
$answer = 1 + 2 + 3 + 4
print $answer
Fairly simple, you think. And not much code, either. But what if you
wanted to add up a thousand numbers? Are you really going to type them
all out like that? Its an awful lot of typing. A loop would make
life a lot simpler. You use them when you want to execute the same code
over and over again.
We'll discuss a few flavours of programming loops, but as the For Loop
is the most used type of loop, we'll discuss those first.
For Loops
Heres a PHP For Loop in a little script. Type it into new PHP
script and save your work. Run your code and test it out.
<?PHP
$counter = 0;
$start = 1;
for($start;
$start < 11; $start++)
{
$counter = $counter + 1;
print $counter . "<BR>";
}
?>
How did you get on? You should have seen the numbers 1 to 10 printed
on your browser page.
The format for a For Loop is this:
for (start value;
end value; update
expression) {
}
The first thing you need to do is type the name of the loop youre
using, in this case for. In between round brackets, you then type your
three conditions:
Start Value
The first condition is where you tell PHP the initial value of your
loop. In other words, start the loop at what number? We used this:
$start = 1
Were assigning a value of 1 to a variable called $start. Like
all variables, you can make up your own name. A popular name for the
initial variable is the letter i . You can set the initial condition
before the loop begins, like we did:
$start = 1
for($start; $start
< 11; $start++) {
Or you can assign your loop value right in the For Loop code:
for($start = 1;
start < 11; start++) {
The result is the same the start number for this loop is 1
End Value
Next, you have to tell PHP when to end your loop. This can be a number,
a Boolean value, a string, etc. Here, were telling PHP to keep
going round the loop while the value of the variable $start is Less
Than 11.
for($start; $start <
11; $start++) {
When the value of $start is 11 or higher, PHP will bail out of the
loop.
Update Expression
Loops need a way of getting the next number in a series. If the loop
couldnt update the starting value, it would be stuck on the starting
value. If we didnt update our start value, our loop would get
stuck on 1. In other words, you need to tell the loop how it is to go
round and round. We used this:
$start++
In a lot of programming language (and PHP) the double plus symbol (++)
means increment (increase the value by one). Its just a short
way of saying this:
$start = $start + 1
You can go down by one (decrement) by using the double minus symbol
(--), but we wont go into that.
So our whole loop reads Starting at a value of 1, keep going
round and round while the start value is less than 11. Increase the
starting value by one each time round the loop.
Every time the loop goes round, the code between our two curly brackets
{ } gets executed:
$counter = $counter + 1;
print $counter . "<BR>";
Notice that were just incrementing the counter variable by 1
each time round the loop, exactly the same as what were doing
with the start variable. So we could have put this instead:
$counter ++
The effect would be the same. As an experiment, try setting the value
of $counter to 11 outside the loop (its currently $counter = 0).
Then inside the loop, use $counter- - (the double minus sign). Can you
guess what will happen? Will it crash, or not? Or will it print something
out? Better save your work, just in case!
To get more practice with the For Loop, we'll write a little Times Table
programme.