Introduction
In any given computer language (PHP is no exception) there has
to be a way to allow the running code to decide between doing 2
different things.
If there wasn't then software would not be able to adapt based
on operating conditions, or it wouldn't be able to decide
between two different users.
In most languages the most basic of these is the
"if" statement, which as its name implies does
something if a given condition is true.
Let's look at a quick example:
$name = "peter";
if($name == "peter")
{
print "Hello peter how are you today?\n";
}
else
{
print "Sorry I don't know you\n";
}
As you can see it's very simple. First we have a string variable
that holds a name. Then we have the "if" statement which says
'if the variable called name holds a value that is equal to
peter then perform the program lines in the first set of curly
brackets, otherwise perform the commands in the second set of
brackets'
The first question you might ask is why do we have a double ==
sign, good question. This is because in PHP everything in a
decision has to result in either true or false, and since we use
= to give a value to a variable, then = will always be true
(trust me, computers work that way ;-) ) and thus we would not
be able to get a false. There are some cases where you have to
use === (3 = in succession) but for most of the time == will do
just fine.
Other Checks?
We don't just have to check to see if two values are equal, we
can also check to see if they are not equal, or greater than and
less than, the following list shows the different operators
'if' understands:
- == - Are variables equal ( $A == $B )
- != - Are variables NOT equal ( $A != $B )
- >= - Is variable 1 greater than or equal to variable 2 ( $A >= $B )
- <= - Is variable 1 less than or equal to variable 2 ( $A <= $B )
- > - Is variable 1 greater than variable 2 ( $A > $B )
- < - Is variable 1 less than variable 2 ( $A < $B )
You can also group one or more decisions together by using
normal brackets, separated by any of the Boolean operators as
shown below:
- && - AND (Both decisions must be true)
- || - OR (Either of the decisions must be true)
These are typically used in the following way:
$fname = "peter";
$lname = "shaw";
if( ($fname == "peter") && ($lname == "shaw") )
{
print "greetings Shawty, you are allowed in
";
}
else
{
print "be-gone stranger, you are banished from here
";
}
This is now saying that fname must be equal to
"peter" AND lname must be equal to "shaw" before I
allow the top code to run, if not then the bottom code will run.
The full range of operators can be found in the php manual
here and as always I strongly encourage you to read and
experiment with different combinations.
1,2,3 hey look I'm counting
The second statement in our journey is the "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 example
below:
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:
- 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.
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.
For this reason we have the while and 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 running\n";
$stop = someFunctionThatReturns1or0();
}
Again, the layout is very simple. In the brackets we have a
decision in exactly the same way as in an 'if' and
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.
While can check anything you like in exactly the
same fashion, EG:
$line = getNextLine();
while($line != "peter")
{
$line = getNextLine();
}
The while loop will keep calling "getNextLine"
until the contents of $line are equal to peter.
NOTE: the "getNextLine" and
"someFunctionThatReturns1or0" calls are for example
only, so don't just cut and paste them, they won't work because
they don't really exist.
Do-while is a reverse version of
while, and anything in the curly braces will be
executed 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 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
'
switch' and dare I say it (Shudders at the
thought) '
goto', which in mine and probably most
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
http://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