Tutorial 11: 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:

<?php

	$fav_fruit = "orange";
	
	if ($fav_fruit == "pineapple") {
	
		echo "YAY! Pineapple is the best.";
		
	} else {
	
		echo "So, you like oranges...";
		
	}
	
?>
Check out the final example
©2019 Brad Hussey