PHP Control Structures 1 - if and else
Written by Mike James   
Thursday, 08 November 2018
Article Index
PHP Control Structures 1 - if and else
Compound statements
Example

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";
$b=2;
}

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.

if

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.

Banner

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.

else

Consider the following if statement:

 if ($a==1){
echo "It is one";
}
echo "No it is not";

This prints "No it is not" if $a isn't one but it also prints the same thing if it is. It is a common early mistake to suppose that the instructions following the if are executed if the condition is false but of course the default flow of control means that they are always executed.

If you want to write an if statement that does one thing if the condition is true and another if the condition is false we need something extra. In English you might write such a condition something like:

 if(coffee is black){
add sugar
}else{
froth coffee
}

This means if the coffee is black you add sugar and you don't froth it. If the coffee isn't black then you don't add sugar but you do froth it. That is, you do the first thing if the condition is true and the second thing if it is false.

The English is almost the same as the PHP if-else instruction:

 if($a==1){
echo "it is one";
}else{
echo "no it isn't";
}

This is a "two-way switch" and either executes the first echo instruction or the second but not both. You can think of the simple if statement introduced earlier as an if-else that just happened to have an empty else part. That is, the simple if is a "do it or don't do it" sort of instruction but the if-else is more a "do this or do that" instruction. If you trace the flow of control with your finger you will find that once again there is a two-way split – there are two routes through the list depending on the condition.

ifelse


This is the shape of the if-else but it looks very messy. With a little pushing and pulling you can see that it is equivalent to the much neater:

ifelsetheory

 

This is how you should formulate the shape of the flow of control in your head. The condition just splits the flow into two branches – one corresponding to true and the other to false – and they join up again once the if statement is complete.

The difficulty with the if statement is mainly due to the need to distort this simple division into something messy to fit it on the page.

<ASIN:0672329166>

<ASIN:0596157134>

<ASIN:1593271735>

<ASIN:1590598628>



Last Updated ( Thursday, 08 November 2018 )