Lecture 17: Else

We’ve figured out how to execute some code if an expression evaluates to TRUE, but what about when the expression evaluates to FALSE? Often you’ll want to execute some code if a certain condition has not been met.

This is where Else comes in to play. Look at Else as an extension of the If statement. The syntax looks like this:

if (expression) {

// code to execute if TRUE

} else {

// code to execute if FALSE

}

Now, let’s take a look at a real-world example:

$fav_fruit = “orange”;

if ($fav_fruit == “pineapple”) {

echo “YAY! Pineapple is the best.”;

} else {

echo “So, you like oranges…”;

}

Not all “equals” are created equal

It’s good to remember that “==” is different than “=”. The former means is equal to while the latter assigns a value to a variable. For example:

if($fav_fruit= “pineapple”)

Will always evaluate to true because the single “=” symbol assigns the value into the variable, rather than comparing it.

DOWNLOAD COURSE FILES HERE
http://www.bradhussey.ca/download-php-course

Comments (5)

Comments are closed.