The if...else statement allows you to execute one block of code if a condition is true and another block if the condition is false.
// Basic if else syntax in PHP
<?php
if (condition) {
echo "Condition is true";
} else {
echo "Condition is false";
}
?>
// Check voting eligibility using if else
<?php
$age = 16;
if ($age >= 18) {
echo "You are eligible to vote.";
} else {
echo "You are not eligible to vote.";
}
?>
If the value of $age is less than 18, the else block executes and displays a message indicating ineligibility.
Change the value of $age and predict the output before running the script.
Enter an age to see which "PHP" block would execute:
(This simulates the PHP logic above using JavaScript)
if_else_statement.phpif...elseecho