<< PHP Operators | PHP Tutorials | PHP Switch >>

PHP If ... Else

Yet another important statement in PHP is the if statement. It does what it looks like it will do. If one thing is _____ to another, do this. For example, if you wanted to know someone's favorite food:

<?php
$favorite_food = 'Cheese';
if ($favorite_food == 'Cheese') {
echo 'I like cheese. Do you?!!<br />';
}

echo 'I like Apples';
?>

What the end users will see:

I like cheese. Do you?!!
I like Apples

Now, if that statement is not equal to the expression inside the parentheses (brackets) it will have a different result:

<?php
$favorite_food = 'Oranges';
if ($favorite_food == 'Cheese') {
echo 'I like cheese. Do you?!!<br />';
}

echo 'I like Apples';
?>

What the end users will see:

I like Apples

The if statement uses mathematical comparisons as well as logical comparisons. Here is a list of the operators from the last page, plus the logical operators ($x = '1' and $y = 1):

  • $x == $y : Equals : true
  • $x === $y : Identical : false
  • $x != $y : Not Equal : false
  • *$x <> $y : Not Equal : false
  • $x !== $y : Not Identical : true
  • $x < $y : Less Than : false
  • $x > $y : Greater Than : false
  • $x <= $y : Less Than or Equal To : true
  • $x >= $y : Greater Than or Equal To : true

Now for the new logical operators (disregard $x and $y):

  • $x and $y : Returns true if both are true
  • $x or $y : Returns true if one is true
  • $x xor $y : Returns true if either one is true, but not both
  • !$x : Returns true if $x is not true
  • $x && $y : Returns true if both are true
  • $x || $y : Returns true if one is true

When I say 'Returns true', it means the if statement will be parsed by the parser. Here is an example of the logical operators in action:

<?php
$w = 'false';
$x = true;
$y = true;
$z = false;
if ($x && $y) {
echo 'x is true and y is true<br />';
}

if ($x == $z) {
echo 'x is equal to z<br />';
}

if ($x !== $w) {
echo 'x is not identical to w<br />';
}

if ($x || $z) {
echo 'x or z was true';
}
?>

This is what the end users will see:

x is true and y is true
x is not identical to w
x or z was true

Notice that the second if statement doesn't show up? Well you should have because it doesn't. $x does not equal $z therefore it won't be written.

PHP Else

PHP also has another useful statement. The else statement. If you haven't already guessed, it means just that. You've already learned that the if statement will execute code if a certain condition is met, but what if the condition isn't met? This is where the else statement comes into play. The else statement does not list any comparisons, it uses the comparison(s) in the if statement and then if none of them were correct, any code in the block for the else statement will be executed. Here is a simple example:

<?php
$number = 1;
if ($number == 1) {
echo '$number is equal to 1';
}
else {
echo '$number is not equal to 1';
}
?>

This is what the end users will see:

$number is equal to 1

Simple enough right? If the $number variable was equal to anything but 1, the else statement would have been executed. Here is a string example:

<?php
$string = 'Hello';
if ($string == 'hello') {
echo '$string is equal to hello';
}
else {
echo '$string is not equal to hello';
}
?>

This is what the end user will see:

$string is not equal to hello

Why? Because when using comparisons, it is CaSe SeNsEtIvE. Oh, what's that now? What if you need to have another condition? Well look at the section below.

Can I Have a Little More Elseif Anyone?

Yep, lame title must not be important. Well it is :3 The elseif statement in PHP can be very important in times, especially if you need to verify more than one condition that can have the same output if the condition is not met. For example, if you wanted to greet someone a different way depending on what time of day it was, the elseif statement would be useful:

<?php
$time = 'Night';
if ($time == 'Morning') {
echo 'Good Morning!';
}
elseif ($time == 'Noon') {
echo 'What? Time to get up?';
}
elseif ($time == 'Evening') {
echo 'Good Evening.';
}
else {
echo 'Sleep? When my face hits the keyboard!';
}
?>

This is what the end users will see:

Sleep? When my face hits the keyboard!

None of the conditions were met in the if...elseif...elseif statements, so it went right to the else statement. You are allowed to use as many elseif statements as you want but there are better ways (see the next lesson).

Conclusion

Conditions are very important in PHP, as in every other programming language. Use them wisely. Also note that elseif and else statements cannot be used unless there is an if statement before them.

<< PHP Operators | PHP Tutorials | PHP Switch >>