dan forys


Learning PHP - Part 4: controlling flow

Posted on 2009-11-02 - Comments

So now we’ve looked at variables and arrays, it’s time to make the code a little bit smarter.

We’ve already seen how the foreach() can loop (iterate) over items in an array, but there are other ways to alter the flow of the code as well.


One of the most common PHP statements is the “if”. In a nutshell, “if” does the following:

if (some condition is true) { run this code }

This means that if whatever is in the brackets is “true”, PHP will run the code in the curly braces. If the condition is not true, PHP will ignore the code in the curly braces.

Here’s a simple example:

$number = 5;
if ($number == 1) { echo 'The number is 1!'; }
if ($number == 5) { echo 'The number is 5!'; }

When the script is run, it will output ‘The number is 5!’.

Note the double equals in the conditions (==). There is an important distinction to make between the '==' and the '=':

  • ‘==’ is used to compare one thing to another. If the ‘things’ are equal, it will evaluate to true. Otherwise it evaluates to false.
  • ‘=’ is used to assign values to variables. (This is actually a simplification, we will come back to its actual meaning in a later post. However, for now you can assume that this is the case)

So, line 2 in the example is saying “$number == 1 evaluates to true, run the rest of the code”. On line 1, we have set “$number = 5″ making the condition evaluate to false, meaning that “The number is 1!” is not output.

However, on line 3, the condition “$number == 5″ evaluates to true, resulting in the string being output.

In place of the ==, you can also use other operators such as > (true if the left is greater than the right), and < (true if the left is less than the right). For example, to test if a number is between 10 and 20:

if ($number > 9 && $number < 21) { echo 'Win!'; }

The double ampersand ‘&&’ means a logical AND, that is the whole expression is true only if the left expression AND the right expression are true. Although we’re looking for a number between 10 and 20, note that in the expressions we are using 9 and 21. This is because we are testing for numbers greater than 9 and less than 21 – this doesn’t include the numbers themselves.

PHP also has >= <= operators; greater than or equals, less than or equals respectively. The same result as the example above looks like this:

if ($number >= 10 && $number <= 20) echo 'Win!';

The conditions needn’t be limited to variables; you can use anything that can evaluate to true or false. PHP has lots of functions that can do this:

if (isset($someVariable)) { echo 'The variable is set'; }

‘isset’ is a PHP function that returns true if a variable has been defined, or false if not. In this example we’re asking it to tell us if the $someVariable is set.

The 'while' loop

There is a way of looping through code ‘while’ a particular condition is true:

$counter = 0;
while ($counter < 11) {
    $counter ++; // This is the same as $counter = $counter + 1
    echo 'Counter is '.$counter.'<br />';

The above code loops around ten times, incrementing the counter each time and outputting it to the screen.

Note that if the condition is false at the start, the while loop is never executed.

The 'for' loop

The for loop is one of the most useful and powerful looping structures in PHP. Its syntax looks rather opaque at first glance, but you’ll soon get used to it. The syntax looks like this:

for ($index = 1; $index < 11; $index ++) {
    echo 'Index is '.$index.'<br />';

The ‘for’ statement itself has three parts, each separated by a semicolon. The first part initialises a variable for us to control the loop; in this case we are setting it to zero. The second part has a condition that will cause the loop to continue as long as it evaluates to true. The third and final part is the operation that gets performed each time the loop repeats; in this case it is incrementing the value of $index.

The result of the code is almost identical to the while loop – it will loop around ten times and echo out the index each time.

The foundation of PHP programming

If you’ve followed the tutorial up to this point, you now have the foundations to build your first dynamic web page.