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

Banner

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.

A small example

The if statement is the core of most PHP programs you simply cannot avoid it. To give you one final simple example consider how you might customise a web page according to the gender of the user. Suppose that we have a variable $Gender which is set to "Male" or "Female" by some other part of the program according to data the user has entered. Using this variable to customise the web page is simple:

 if( $Gender== "Male"){
echo "Welcome Master";
}else{
echo "Welcome Mistress" ;
}

Notice that this only works because $Gender can only be Male or Female – no additional test is carried out to make sure that the variable is indeed set to Female. This is the sort of thing that often occurs in programming. If you want to be 100% sure that your program will still work if someone introduces a third category for gender – then you need to write two if statements:

 if( $Gender== "Male"){
echo "Welcome Master";
}
if( $Gender== "Female"){
echo "Welcome Mistress" ;
}

Of course to deal with a third, or fourth category you would need additional if statements.

Building programs with ifs

If you are getting used to the idea of following the flow of control you should be able to see that building a more complicated program is mostly a matter of building more a complicated flow of control. You can think of it almost as if you were building a model railway set, putting together a track that branches at each if and joins up again so that each branch of the if continues on the single main line – the default flow of control.

With just the if and the if-else statements as introduced in this article you can build any shape of railway track you need to. That is, you don't really need to learn any other versions of the if statement. However PHP offers additional conditional statements – the elseif and switch. Don't move on to these until you have mastered the simple if and feel confident that given any program you can trace and understand the flow of control.

When you are ready, move on to more advanced conditional statements.

More PHP

Banner


Developing with PHP and Eclipse (Indigo)

Find out how to make PHP development easy with the latest version of Eclipse, Indigo, and how to set up local and remote debugging.



Trying the PhpStorm 2.0 IDE

What does JetBrains IDE PhpStorm 2.0 offer the professsional PHP Developer? We put it through its paces using the free 30-day trial.


Other Articles

<ASIN:0596514018>

<ASIN:0596005601>

<ASIN:0596006306>

<ASIN:0596101015>

ifelse



Last Updated ( Monday, 15 February 2010 )
 
 

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