How if Else Statement Works in PHP?
In the PHP coding language, there are three conditions to be applied to assign a value to a variable. The first is the if condition, which can have one or more strings of variables as its operands. The second is the elseif condition, which also has several parameters. The third condition is the else statement. The if statement checks the value of a variable and executes the statements inside the if block when the variable meets or surpasses the required criteria.
php elseif
<?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";
}
?>
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";
}
?>
Source: www.php.net
php if
<?php
if ($a > $b) {
echo "a is bigger than b";
$b = $a;
}
?>
Source: www.php.net
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';
}
php else
<?php
if ($a > $b) {
echo "a is greater than b";
} else {
echo "a is NOT greater than b";
}
?>
Source: www.php.net
php if
<?php
if ($a > $b)
echo "a is bigger than b";
?>
If the statement would basically decide which code to execute depending on the values of variables while the if-else statement is used to make decisions on the condition of certain variables and then proceed accordingly with other conditionals.