Introduction
An if statement helps your program decide what to do based on a condition. It lets your code choose between different paths.
An if statement helps your program decide what to do based on a condition. It lets your code choose between different paths.
<?php if (condition) { // code to run if condition is true } elseif (another_condition) { // code to run if the first condition is false and this one is true } else { // code to run if all above conditions are false } ?>
The condition is a statement that is either true or false.
The elseif and else parts are optional.
<?php if ($age >= 18) { echo "You can vote."; } ?>
<?php if ($score >= 90) { echo "Grade A"; } elseif ($score >= 80) { echo "Grade B"; } else { echo "Grade C or below"; } ?>
This program checks the temperature and prints a message based on its value.
<?php $temperature = 25; if ($temperature > 30) { echo "It's hot outside."; } elseif ($temperature >= 20) { echo "The weather is nice."; } else { echo "It's cold outside."; } ?>
Only one block of code inside the if-elseif-else runs, the first true condition found.
If no condition is true and there is no else, nothing happens.
If statements let your program choose what to do based on conditions.
Use elseif to check multiple conditions in order.
Use else to run code when no conditions are true.
if statement do in PHP?if (condition) { ... }. if condition { echo 'Yes'; } misses parentheses, C uses alternate syntax but missing parentheses, D uses square brackets which is invalid.$x = 10;
if ($x > 10) {
echo 'Greater';
} elseif ($x == 10) {
echo 'Equal';
} else {
echo 'Less';
}
if $a > 5 {
echo 'Big';
} else {
echo 'Small';
}if $a > 5 is missing parentheses.$score = 75;
if ($score >= 90) {
echo 'A';
} elseif ($score >= 80) {
echo 'B';
} elseif ($score >= 70) {
echo 'C';
} else {
echo 'F';
}
Pass the test and this pattern moves unknown