else if php code example
Example 1: php elseif
$b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Example 2: php if else
$b) {
echo "a is bigger than b";
} elseif ($a == $b) {
echo "a is equal to b";
} else {
echo "a is smaller than b";
}
?>
Example 3: php if
$b) {
echo "a is bigger than b";
$b = $a;
}
?>
Example 4: php conditionals
#Conditionals
greater than
<= less than or equal to
>= greate than or equal to
!= not equal to
!== not identrical
*/
$num = 5;
#if($num == 5){
# echo '5 passed';
#}elseif($num == 6){
# echo '6 passed';
# }else{
# echo'did not pass';
# }
#nesting If Statements
if($num>4){
if($num < 10){
echo "$num passed
";
}
}
/*
Logical operators can be used
instead of what is above
and &&
or ||
xor means one has to be true but not both
*/
if($num >4 && $num <10){
echo "$num passed using Logical operators";
}
/*
Switch tests for value and then find
a match and do something
*/
$favColor = 'pink';
switch($favColor){
case 'red':
echo '
Your favorite color is red';
break;
case 'green':
echo '
OH NO!! Your favorite color is green';
break;
case 'blue':
echo '
Kool!! Your favorite color is blue';
break;
default:
echo '
Yikes! Your favorite color is something else';
}
?>
Example 5: php if elseif
$a = random_int(0, 10);
$b = random_int(0, 10);
if ($a > $b) {
echo 'a is greater than b';
} elseif ($a == $b) {
echo 'a is equal to b';
} else {
echo 'a is less than b';
}
Example 6: php else
$b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
?>