PHP Tutorials

Tutorial 5 - If Statements

As you may have wondered already, whilst we have explored how to handle various strings and numbers, we haven't yet looked into how we can use PHP to make decisions, and then perform various actions based on the results of those actions. This sounds a little complicated at first (especially if PHP is the first scripting/programming language you're learning), however PHP "If" statements are very useful and powerful. For example (in a non-code form), you could use PHP to judge whether a member has his birthday today, and if so send him a birthday e-mail.

As was mentioned in the fourth tutorial (about operators), an IF statement effectively runs a test, and then carries out particular actions depending on the results of that test (the test will literally return either a "true" or "false" value). The syntax of an IF statement is below:


<?php

if( /* some test is TRUE */ )
{
    
// Carry out actions in this section
}

// A quick example: if 5 is greater than 2, then echo "5 is indeed greater than 2"
if( )
{
    echo 
'5 is indeed greater than 2';
}

// Obviously this example is a bit limited (because there's no real point to it),
// although the above will hopefully show how an IF statement works

?>

The above example only concerns what happens if the test is true. However in many occasions, the PHP coder will want to do one thing if the test is true, and another thing if the test is false. This is where "else" statements come into play:


<?php

if( /* some test is TRUE */ )
{
    
// IF the test is TRUE:
    // Carry out actions in this section
}

else
{
    
// If the test is FALSE:
    // Carry out actions in this section
    // Notice how you don't have to carry out any test with the else statement - it
    // simply reacts to the if statement (i.e. if the if statement is false, the
    // else statement will be automatically carried out)
}

// As can be seen, this allows more flexibilty because the coder can now carry out
// different actions depending on the result of the IF statement test

// Another quick example: imagine a new member registers at a message board. To
// prevent confusion, we wouldn't want the new member's name to be "admin"...

// The $newMemberName variable would usually come from the form that the new member
// submits, however in this example we'll just assume that the member selected
// their name (invalidly) to be "admin":
$newMemberName 'admin';

if( 
$newMemberName == 'admin' )
{
    die(
"Error! Sorry, however you have chosen a reserved name. Please re-register
         with a new name."
); // This is a particular function (various string
                             // functions are explained in tutorial 3) - the "die()"
                             // function stops the script running, and outputs an
                             // error message
}

else
{
    echo 
'Registration successful! You will receive membership details via e-mail
           soon.'
// Obviously no mail would be sent out with this script (as it's
                    // just an example and doesn't try to send out e-mail); this
                    // is just to illustrate further how an IF statement works
}

?>

So now we are able to carry out two different actions depending on the result of a test. However even that may not be enough options! What if (for example) the registration script (above) wants to check the $newMemberName against more than one value? In this case, we use something called "elseif". The elseif statement acts in the same way to an if statement (i.e. you still have to carry out a test with the elseif statement) - the only difference is that the "if" statement will always come first, and then any elseif statements (if applicable), and then the "else" statement always finishes things off:


<?php

if( /* some test is TRUE */ )
{
    
// IF the above is true, then carry out the code in this section
}

elseif( 
/* a different, but related test is TRUE */ )
{
    
// IF the above isn't true, then carry out the code in this section
}

else
{
    
// IF none of the above if or elseif statements are true, then carry out the
    // code in this section
}

// Lets have a look at a quick example. Imagine that your website is pretty
// successful, hence you want to have a rotating advertising slot.

$number rand(1,5); // This "rand" function generates random numbers; in this case,
                     // a random number between 1-5 inclusive

// Okay, so we'll start off by seeing whether the random number is 1 (and, if it is,
// display the 1st advert)
if( $number == )
{
    
logAdvert(1); // This is a made up function; it doesn't do anything; imagine,
                  // though, that is simply logs that the 1st advert is being shown
                  // (for statistical reasons)
    
echo '<a href="http://phpmission.com"><img src="Advert1.gif" width="468"
          height="60" alt="PHP Mission" /></a>'
;
}

// IF the $number isn't 1, do "elseif" statements until we 'find' the correct
// number
elseif( $number == )
{
    
logAdvert(2);
    echo 
'<a href="http://www.ianwillis.co.uk"><img src="Advert2.gif" width="468"
          height="60" alt="Ian Willis" /></a>'
;
}

elseif( 
$number == )
{
    
logAdvert(3);
    echo 
'<a href="http://www.websitenumber3.com"><img src="Advert3.gif"
          width="468" height="60" alt="Website Number 3" /></a>'
;
}

elseif( 
$number == )
{
    
logAdvert(4);
    echo 
'<a href="http://www.websitenumber4.com"><img src="Advert4.gif"
          width="468" height="60" alt="Website Number 4" /></a>'
;
}

// If $number isn't 1, 2, 3 or 4, it must be 5 - however let's just do an "else"
// statement to finish up
else
{
    
logAdvert(5);
    echo 
'<a href="http://www.websitenumber5.com"><img src="Advert5.gif"
          width="468" height="60" alt="Website Number 5" /></a>'
;
}

// End of example! Basically that's how a very basic advert rotation script would
// work.

// As another quick example, remember in tutorial 4 how we looked at logical and
// comparison operators? This is where you would use them (i.e. in if statements).
// Lets just quickly look at how they'd be used:

$x 3;
$y 4;

if( 
$x == && $y )
{
    echo 
'$x is equal to 3 and $y is greater than 5';
    
// In this case, $x == 3 BUT $x isn't greater than 5; hence this bit of code
    // wouldn't be executed
}

elseif( 
$x == && $y )
{
    echo 
'$x is equal to 3 and $y is greater than 3';
    
// In this case, $x == 3 AND $x is greater than 3; hence this bit of code would
    // be executed. In other words, this elseif statement is TRUE, hence "$x is
    // equal to 3 and $y is greater than 3" would be echo'ed out.
}

else
{
    echo 
'$x isn\'t equal to 3 and/or $y isn\'t greater than 5';
    
// In this case, the "elseif" statement returns true; hence this bit of code
    // wouldn't be executed
}

?>

And there we have it! Hopefully now you can see how to use if, elseif and else statements correctly. And combining them with the logical and comparison operators in tutorial 4 will hopefully help you to realise how a real life PHP script sort of pieces itself together.