The if statement in PHP is used to execute code only when a specified condition evaluates to true. It plays a critical role in decision-making and controlling the flow of a PHP program.
elseif and elseif statements allow complex decision logicThe PHP if statement checks a condition and executes the block of code only if that condition is satisfied. If the condition is false, the code inside the block is skipped.
Test the logic: Enter a numeric Score (0-100) to see the if...elseif logic in action.
This simulates how PHP would evaluate your input based on score thresholds.
// Check if the user is eligible to vote
<?php
$age = 20;
if ($age >= 18) {
echo "You are eligible to vote.";
}
?>
// Decide pass or fail based on marks
<?php
$marks = 40;
if ($marks >= 50) {
echo "You passed the exam.";
} else {
echo "You failed the exam.";
}
?>
// Display greeting based on time
<?php
$time = 15;
if ($time < 12) {
echo "Good Morning!";
} elseif ($time < 18) {
echo "Good Afternoon!";
} else {
echo "Good Evening!";
}
?>
// Check both age and citizenship for voting
<?php
$age = 25;
$citizen = true;
if ($age >= 18) {
if ($citizen) {
echo "You can vote in elections.";
} else {
echo "You must be a citizen to vote.";
}
}
?>
Based on the input values, PHP evaluates each condition and executes only the matching block. This ensures the program responds differently depending on user data or system state.
Imagine a decision tree where each condition branches the execution path. Nested if statements allow PHP to evaluate multiple layers of logic step by step.
if blocks when possibleelseif instead of multiple independent if statements