PHP Control Structures 1 - if and else
PHP Control Structures 1 - if and else
Friday, 29 January 2010
Article Index
PHP Control Structures 1 - if and else


Getting to grips with programming or a new language is a matter of mastering the flow of control. This is the key idea in programming and understanding it makes the difference between a programmer and a non-programmer. If you already know about flow of control, because you program in another language, then skip to the summaries of PHP's control structures.

It is also assumed that you are happy with the ideas of variables and expressions – if not see PHP variables and expressions for complete beginners.



Default flow of control

A program is a list of instructions and when you run the program these instructions are obeyed one after another, nearly always reading from left to right and top to bottom of the list. This is the default flow of control - each instruction has its moment to be obeyed and hence to be in control of the computer.

In PHP the default flow of control is what you get, by default. For example in:

 echo "Hello";
echo "World";
echo "final instruction";

each of the echo instructions are obeyed one after the other from the top of the list to the bottom.

The default flow of control is so natural that it's easy to just not notice it. If you take your finger and follow down the instructions in the order in which they are obeyed then you will trace out a straight line – this is the "shape" of the default flow of control.



Selecting when to do something – the if

The first change to the default flow of control is when you only want something to happen if a condition is satisfied. In everyday life we often encounter conditional instructions:

"if the coffee is black then add sugar"

In this case the instruction "add sugar" is only obeyed if the condition, i.e. "the coffee is black", is true. Notice that this conditional is part of the default flow of control in the sense that it is part of the list of instructions:

pour the coffee
if the coffee is black then add sugar
drink the coffee

In PHP a conditional or if statement is written


and the instruction is only carried out if the condition is true. In a syntax that is closer to PHP you might write the coffee conditional as:

if(coffee is black) add sugar;

Of course "coffee is black" isn't a PHP conditional and "add sugar" isn't a PHP instruction – but the idea still holds good.


To make use of if statements and other similar control statements we need to find out how to write a conditional in PHP. In most cases the sort of condition you want to write is a comparison between two values. You want to know if the value in the variable $a is zero or if it is bigger than the value in variable $b, for example.

To make these comparisons possible there are a range of suitable operators which can be used to form conditional or Boolean expressions which evaluate to true or false.

For example, $a==$b is true if the variables store the same value. Notice that the comparison operator is a double equals sign that is = followed by another = without a space. Other comparison operators are fairly obvious; $a<$b is only true if the value in $a is less than the value in $b and $a>$b is only true if the value in $a is greater than $b and so on. The following table summarises the most commonly used:

$a==b$ true if equal value
$a!=$b or $a<>$b true if not equal
$a<$b true if $a less than $b
$a>$b true if $a greater than $b
$a<=$b true if $a less than
or equal to $b
$a>=$b true if $a greater than
or equal to $b

If you look at the documentation you will discover that there is also a triple equals which is true if the two values are equal and of the same type, i.e. no automatic type conversion is performed before the comparison.

As well as simple numerical comparisons you can also test to see if strings are equal or not. For example if you store some text in a variable:

 a$= "APPLE";

then the condition: a$== "APPLE" is true. You can use other comparison operators such as < and > with strings but exactly how these work is complicated and most of the time you only test strings for equality using == or inequality using != or <>.

For example, assuming the previous assignment:

 a$ != "ORANGE"

is true, i.e. a$ isn't equal to ORANGE and

 a$ == "ORANGE"

is false.

The bigger if – compound statements

Now that we know how to write conditions we can write full PHP if statements. For example:

if($a==1) echo "It is one";

This outputs "It is one" only if $a contains the value 1.

This is the basic PHP if statement but there are more possibilities as you can't do everything with such a simple form of the if statement.

The first extra facility we need is to be able to conditionally execute more than just one instruction.

You might think that PHP would introduce another more complicated form of the if to accommodate multiple instructions but no! There is a simpler and more universal way of making things work.

Any set of PHP instructions grouped between curly brackets i.e. { } is considered to be a single PHP instruction – a compound instruction. Anywhere you can use a single PHP instruction you can use a compound instruction. So for example:

if($a==1){echo "It is one"; $b=2;}

if $a contains 1 then both the echo and the assignment of 2 into $b are carried out in the usual order.

You can have as many instructions within a compound statement as you like and writing an if statement as in the last example soon becomes difficult to read. There are lots of different conventions about how programs should be formatted but the key idea is that they should be formatted so as to be easy to read and understand.

In the case of an if what matters is that you can clearly see the instructions which are conditional, i.e which will be carried out if the condition is true and skipped otherwise. Most PHP programmers prefer to format an if as follows:

 if ($a==1){
echo "It is one";

The indenting helps to see the list of instructions that are part of the if.

A general principle is that whenever you start any if or any other control structure then indenting can help show where it starts and ends. Notice that you can check that everything is correct by looking to find the bracket that lines up underneath the letter i of the if.

If you trace your finger over the instructions in the order that they are executed then you will find you have two possible routes – one when the condition is true and one when it is false. The if splits the default flow of control into two possibilities one that jumps over the instructions in the if and one that executes them.


This is the shape of the flow of control of the simple if.

If you are using Eclipse then you will discover that it tries to help you keep things tidy. When you type "if(condition){" and a carriage return it immediately adds a new line, puts a closing } on the line below and adds an indent to everything you subsequently type.


This layout is so commonplace that it tends to be used even if there is only a single instruction to place inside the curly brackets.





Last Updated ( Monday, 15 February 2010 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.