In PHP, conditional statements such as if
, else
, and elseif
are used to execute different blocks of code based on certain conditions. These statements allow you to control the flow of your program by specifying which code should run depending on whether a condition evaluates to true
or false
.
1. The if
Statement
The a if
statement is used to test a condition. If the condition is true, the block of code inside the if
statement is executed. If the condition is false, the code block is skipped.
Syntax:
Example:
In this example, if $age
is greater than or equal to 18, the message "You are eligible to vote." will be displayed.
2. The else
Statement
The else
statement is used in conjunction with a if
statement. It provides an alternative block of code to execute when the if
condition is false.
Syntax:
Example:
In this example, since $age
is less than 18, the output will be "You are not eligible to vote."
3. The elseif
Statement
The A elseif
statement allows you to test multiple conditions. If the condition in the if
statement is false, it will check the condition in the elseif
statement. You can have multiple elseif
statements in a chain. If none of the conditions are true, the else
block (if provided) will be executed.
Syntax:
Example:
In this example, since $age
is 25, the message "You are a young adult." will be displayed.
4. Multiple elseif
Statements
You can have as many elseif
statements as needed to check different conditions.
Example:
In this example, the score is 85, so the output will be "Grade B" because the score is greater than or equal to 80 but less than 90.
5. Using Logical Operators
You can combine multiple conditions in if
, elseif
, and else
statements using logical operators like &&
(AND), ||
(OR), and !
(NOT).
Example with AND (&&
):
Here, both conditions must be true for the code inside the if
block to execute.
Example with OR (||
):
Here, the condition will be true if it's either Saturday or Sunday.
6. Ternary Operator
PHP also offers a shorthand way to write if
, elseif
, and else
statements using the ternary operator. The ternary operator is often used for simple conditions.
Syntax:
Example:
In this example, the condition ($age >= 18)
is checked. If true, "You are eligible to vote." is displayed, otherwise "You are not eligible to vote." is shown.
7. Switch Statement (Alternative)
For multiple conditions, the a switch
statement can sometimes be more efficient than using multiple if
, elseif
, and else
statements. The switch
statement evaluates an expression and compares it with multiple case
values.
Syntax:
Example:
In this example, since $day
is "Monday", the output will be "Start of the work week."
Summary
if
: Executes a block of code if the condition is true.else
: Executes a block of code if the condition in theif
statement is false.elseif
: Allows you to check multiple conditions if the previousif
orelseif
was false.- Logical Operators (
&&
,||
,!
) can be used to combine conditions. - The ternary operator is a shorthand for
if-else
and can be used for simpler conditions. - The
switch
statement is an alternative to multipleif-else
when checking the value of a single expression against many possible cases.