Jump to: navigation, search

Php elseif/else if

From w3cyberlearnings

Contents

elseif condition

elseif is combined with if and else. When the if condition is false, the else if condition will be executed, and when the execution meets the condition witin the else if. The statement within the else if will be executed.

Syntax if

if(expr)
 statement
else if(expr)
 statement
else 
 statement

Example 1

<?php

$a = 300;

if ($a + 40 >= 500) {
	echo '$a+40 >=100';
} else if ($a + 100 <= 500) {
	echo '$a + 100 <=500';
}
?>


Output

$a + 100 <=500

Example 2

<?php

$name = 'Bob';
$age = 30;
if ($name == "Bob") {
	echo 'Your name is Bob <br/>';
} else if ($age == 30) {
	echo 'You are 30 year old <br/>';
} else {
	echo 'No record';
}
?>

Output

Your name is Bob 

Example 3

<?php

$name = 'Bob';
$age = 30;
if ($name != "Bob") {
	echo 'Your name is Bob <br/>';
} else if ($age == 30) {
	echo 'You are 30 year old <br/>';
} else {
	echo 'No record';
}
?>

Output

You are 30 year old 

Related Links


Navigation
Web
SQL
MISC
References