Conditional Statements in PHP with Example

phpIn PHP, if and elseif statements are conditional statements.  They work with comparison operators.  Conditional statements form a block where further code execution depends upon result of a condition.  The condition will return a boolean value (true or false).

If statement is always the first statement in the series of conditional statements.  If first statement fails to meet a certain condition, then it will jump to the next conditional statement which is elseif statement.  The last statement is always else statement in that series.  The code inside else statement always gets executed if both the previous conditional statements (if and elseif) are failed to meet a certain condition.

A very simple example is given below.  You can change the values of $n1, $n2, and $n3 to get the desired output.  By default, none of the values are equal, so it will go to else block.

$n1 = 5;
$n2 = 6;
$n3 = 7;

if ($n1==$n2) {
	echo "n1 and n2 are equal.";
elseif ($n1==$n3) {
	echo "n1 and n3 are equal.";
	echo "None of them are equal.";