If statements
In PHP, if
statements are used to execute a block of code if a specified condition is true. They can be combined with elseif
and else
clauses to create more complex conditional logic. Here's the basic syntax of an if
statement:
if (condition) {
// Code to execute if the condition is true
}
If you want to execute different code blocks based on multiple conditions, you can use elseif
and else
statements:
if (condition1) {
// Code to execute if condition1 is true
} elseif (condition2) {
// Code to execute if condition1 is false and condition2 is true
} else {
// Code to execute if both condition1 and condition2 are false
}
Here's a simple example:
$age = 25;
if ($age >= 18) {
echo "You are an adult.";
} else {
echo "You are a minor.";
}
In this example, if the variable $age
is greater than or equal to 18, the message "You are an adult." is printed; otherwise, "You are a minor." is printed.