if, else, elseif

Consists of a boolean expression (evaluating to true or false), followed by one or more statements

 

if(test condition) {

//execute this code block if TRUE

}


Can be followed by an (optional) else statement.

<?php

	if (TRUE) { //will always be TRUE

		echo "TRUE" ;

	} else {

		echo "FALSE" ;
	}
?>

 

elseif allows for an additional test condition statements

<?php
	$myVar = rand(1,5);//generate a randon number in the range 1 to 5

	if ($myVar == 1) {
		echo "Random generated number was: $myVar" ;
	}
	elseif($myVar == 2) {
		echo "Random generated number was: $myVar" ;
	}
	elseif($myVar == 3) {
		echo "Random generated number was: $myVar" ;
	}
	elseif($myVar == 4) {
		echo "Random generated number was: $myVar" ;
	} else {
		echo "Random generated number was: $myVar" ;
	}
?>

Leave a Reply