1,2,3 hey look I’m counting
The second statement in our journey is the “
loop, the whole purpose of which is to repeatedly do something
until a given condition is true. In some ways it’s kind of like
a repeating “
below:
for
”loop, the whole purpose of which is to repeatedly do something
until a given condition is true. In some ways it’s kind of like
a repeating “
if
” statement as shown in the examplebelow:
for($counter=0;$counter<10;$counter++)
{
print $counter . "n";
}
At first glance this looks very complicated, but if we take it
apart bit by bit, you’ll see it suddenly makes a lot of sense:
apart bit by bit, you’ll see it suddenly makes a lot of sense:
- For( – The statement start
- $counter = 0; – Set the default start value of our counting
variable - $counter<10; – The decision part that decides when to
stop, translated into =’if’ speak “if $counter is less than 10
keep going” - $counter++) – What to do to update the control variable (In
this case add 1) =(we covered ++ and – in maths &
numbers)
So, as you can see the loop contains 3 parts, an initialization
part, a when to stop check, and an update part, all together
providing you fine control over how many times something
happens.
part, a when to stop check, and an update part, all together
providing you fine control over how many times something
happens.
What if I don’t know in advance how many there is going to be?
That’s a perfectly valid question. You can’t always know in
advance how many of a given count there are going to be, or even
where that count will start.
advance how many of a given count there are going to be, or even
where that count will start.
For this reason we have the while and
and just as they sound by their names, they keep performing a
given task until something is true, eg.
do-while loop
operators,and just as they sound by their names, they keep performing a
given task until something is true, eg.
$stop = 0;
while($stop != 1)
{
Print "Still runningn";
$stop = someFunctionThatReturns1or0();
}
Again, the layout is very simple. In the brackets we have a
decision in exactly the same way as in an ‘
yes you can group using && and || too, and then we have a block
of code inside our curly brackets, that keeps going until our
fictitious function returns a 1.
decision in exactly the same way as in an ‘
if
‘ andyes you can group using && and || too, and then we have a block
of code inside our curly brackets, that keeps going until our
fictitious function returns a 1.
While
can check anything you like in exactly thesame fashion, EG:
$line = getNextLine();
while($line != "peter")
{
$line = getNextLine();
}
The while loop will keep calling “
until the contents of
NOTE: the “
“
only, so don’t just cut and paste them, they won’t work because
they don’t really exist.
getNextLine
”until the contents of
$line
are equal to peter.NOTE: the “
getNextLine
” and“
someFunctionThatReturns1or0
” calls are for exampleonly, so don’t just cut and paste them, they won’t work because
they don’t really exist.
Do-while
is a reverse version ofwhile
, and anything in the curly braces will beexecuted at least once before the check to stop is encountered.
Using our example above:
do
{
$line = getNextLine();
} while($line != "peter");
As you can see, the first thing this does is prevent us from
having to load our variable the first time, because we know that
the code will run at least one time, which
loop you use will depend on exactly how your program logic is to
function.
having to load our variable the first time, because we know that
the code will run at least one time, which
while
loop you use will depend on exactly how your program logic is to
function.
Summary
We covered the most commonly used control statements here, but
there are other controls available in the language, such as
‘
thought) ‘
professional programmers minds should never have been invented
in the first place. There are also a couple of variations on the
standard if statement that I’ve not covered. As always refer to
the PHP manual’s section on control statements at https://phpbuilder.com/manual/en/language
.control-structures.php and experiment with the others. I
would also strongly recommend reading the user submissions in
this section, even if you haven’t in any of the others, some of
the tips and shortcuts in this section show some extremely cool
tricks that make PHP come alive.
there are other controls available in the language, such as
‘
switch
‘ and dare I say it (Shudders at thethought) ‘
goto
‘, which in mine and probably mostprofessional programmers minds should never have been invented
in the first place. There are also a couple of variations on the
standard if statement that I’ve not covered. As always refer to
the PHP manual’s section on control statements at https://phpbuilder.com/manual/en/language
.control-structures.php and experiment with the others. I
would also strongly recommend reading the user submissions in
this section, even if you haven’t in any of the others, some of
the tips and shortcuts in this section show some extremely cool
tricks that make PHP come alive.
Until next time
Don’t get stuck in a loop.
Shawty
Shawty