Conditional statements are fundamental constructs in programming that allow different blocks of code to be executed based on whether a specified condition evaluates to true or false. They enable programs to make decisions, leading to more dynamic and responsive applications. PHP, like most programming languages, provides several types of conditional statements: `if`, `else`, `elseif`, and `switch`.
1. The `if` Statement:
The `if` statement is the simplest form of a conditional statement. It executes a block of code only if a specified condition is true.
Syntax:
```php
if (condition) {
// Code to be executed if the condition is true
}
```
2. The `if-else` Statement:
The `if-else` statement provides an alternative block of code to be executed if the `if` condition evaluates to false. This ensures that one of two blocks of code will always be executed.
Syntax:
```php
if (condition) {
// Code to be executed if the condition is true
} else {
// Code to be executed if the condition is false
}
```
3. The `if-elseif-else` Statement:
This statement is used to test multiple conditions. It checks the `if` condition first. If it's false, it moves to the `elseif` condition. This process continues until a true condition is found, or if none are true, the `else` block (if present) is executed. There can be multiple `elseif` clauses.
Syntax:
```php
if (condition1) {
// Code to be executed if condition1 is true
} elseif (condition2) {
// Code to be executed if condition2 is true
} elseif (condition3) {
// Code to be executed if condition3 is true
} else {
// Code to be executed if none of the above conditions are true
}
```
4. The `switch` Statement:
The `switch` statement is an alternative to long `if-elseif-else` chains when you are comparing a single variable against multiple possible values. It checks the value of an expression against several `case` values. If a match is found, the corresponding block of code is executed. The `break` keyword is crucial to exit the switch block after a match, preventing "fall-through" to subsequent cases. The `default` case is optional and executes if no other case matches.
Syntax:
```php
switch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
default:
// Code to be executed if no case matches
break;
}
```
`switch` statements are often preferred over `if-elseif-else` for clarity and efficiency when dealing with many discrete values for a single variable, as they can sometimes be optimized better by the interpreter.
Conditional statements often utilize comparison operators (`==`, `!=`, `<`, `>`, `<=`, `>=`) and logical operators (`&&` (AND), `||` (OR), `!` (NOT)) to construct complex conditions.
Example Code
<?php
echo "<h2>PHP Conditional Statements Examples</h2>";
// Example 1: if statement
$age = 20;
echo "<h3>1. The 'if' Statement</h3>";
if ($age >= 18) {
echo "You are an adult.<br>";
}
// Example 2: if-else statement
$temperature = 25;
echo "<h3>2. The 'if-else' Statement</h3>";
if ($temperature > 30) {
echo "It's a hot day!<br>";
} else {
echo "It's not too hot.<br>";
}
// Example 3: if-elseif-else statement
$score = 85;
echo "<h3>3. The 'if-elseif-else' Statement</h3>";
if ($score >= 90) {
echo "Grade: A<br>";
} elseif ($score >= 80) {
echo "Grade: B<br>";
} elseif ($score >= 70) {
echo "Grade: C<br>";
} else {
echo "Grade: F<br>";
}
// Example 4: switch statement
$dayOfWeek = "Wednesday";
echo "<h3>4. The 'switch' Statement</h3>";
switch ($dayOfWeek) {
case "Monday":
echo "It's the start of the week.<br>";
break;
case "Wednesday":
echo "It's mid-week.<br>";
break;
case "Friday":
echo "Almost weekend!<br>";
break;
default:
echo "It's a regular day.<br>";
break;
}
// Example with logical operators
$isRaining = true;
$hasUmbrella = false;
echo "<h3>5. Conditional with Logical Operators</h3>";
if ($isRaining && $hasUmbrella) {
echo "You are prepared for the rain.<br>";
} elseif ($isRaining && !$hasUmbrella) {
echo "You might get wet.<br>";
} else {
echo "No rain today.<br>";
}
?>








Conditional Statements (if, else, switch)