if and php code example
Example 1: php if else
<?php
if ($a > $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
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
Example 3: php conditionals
#Conditionals
<?php
$num = 5;
if($num>4){
if($num < 10){
echo "$num passed<br>";
}
}
if($num >4 && $num <10){
echo "$num passed using Logical operators";
}
$favColor = 'pink';
switch($favColor){
case 'red':
echo '<br>Your favorite color is red';
break;
case 'green':
echo '<br>OH NO!! Your favorite color is green';
break;
case 'blue':
echo '<br>Kool!! Your favorite color is blue';
break;
default:
echo '<br>Yikes! Your favorite color is something else';
}
?>
Example 4: 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 5: if not php
$a = true;
if(!$a) {
echo "False";
} else {
echo "True";
}
Example 6: php if
<?php
if ($a > $b)
echo "a is bigger than b";
?>