![]() Join Up! 96813 members and counting! |
|
|||
Loops & Decisions in PHP - The ABC's of PHP Part 8
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:
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:
You can also group one or more decisions together by using
normal brackets, separated by any of the Boolean operators as
shown below:
These are typically used in the following way:
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:
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:
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.
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:
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:
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
The ABC's of PHP
Introduction to PHPWhat do I need to make it work? Basic Script Building in PHP How Variable Am I? Strings & Text Math & Number Handling in PHP Introduction to Arrays and Hashes in PHP Loops and Decisions in PHP Advanced String Processing - How Regular Are Your Expressions |