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
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";
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
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
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
== - 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
&& - 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 ";
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
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:
$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
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.