Learn PHP basics with this beginner-friendly guide. Understand PHP syntax, variables, functions, and more to start building dynamic web applications.
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
if (condition) {
code to be executed if this condition is true;
} elseif (condition) {
code to be executed if this condition is true;
} else {
code to be executed if all conditions are false;
}
The following example would display a is bigger than b if $a is bigger than $b:
<?php
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";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP - The if...elseif....else Statement</title>
</head>
<body>
<?php
// i for Minutes with leading zero, s for Seconds with leading zero
echo "Today is " . date("d/m/Y") . "<br>";
echo "Minutes:" . date("i");
echo "<br> Seconds:" . date("s") . "<br>";
//
// if..elseif..else
//
$t = date("H");
if ($t < "10") {
echo "Have a good morning!";
} elseif ($t < "20") {
echo "Have a good day!";
} else {
echo "Have a good night!";
}
?>
</body>
</html>
further reading: