PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques

Conditional statements in PHP are used to perform different actions based on different conditions. These statements help control the flow of the program, allowing certain code blocks to execute only if specified conditions are met. Here’s an in-depth look at the different types of conditional statements in PHP:

1. if Statement

The if statement is the most basic conditional statement, which executes a block of code if a specified condition is true.

Syntax:

				
					if (condition) {
    // Code to be executed if condition is true
}

				
			

Example:

				
					$age = 20;
if ($age >= 18) {
    echo "You are an adult.";
}

				
			

2. 'else' Statement

The ‘else‘ statement is used together with ‘if‘. It executes a block of code if the condition in the ‘if‘ statement is false.

Syntax:

				
					if (condition) {
    // Code to be executed if condition is true
} else {
    // Code to be executed if condition is false
}

				
			

Example:

				
					$age = 16;
if ($age >= 18) {
    echo "You are an adult.";
} else {
    echo "You are a minor.";
}

				
			

3. 'elseif' Statement

The ‘elseif‘ statement is used to specify a new condition if the first condition is false. You can use multiple ‘elseif‘ statements to check multiple conditions.

Syntax:

				
					if (condition1) {
    // Code to be executed if condition1 is true
} elseif (condition2) {
    // Code to be executed if condition1 is false and condition2 is true
} else {
    // Code to be executed if condition1 and condition2 are both false
}

				
			

Example:

				
					$score = 75;
if ($score >= 90) {
    echo "A";
} elseif ($score >= 80) {
    echo "B";
} elseif ($score >= 70) {
    echo "C";
} else {
    echo "F";
}

				
			

4. switch Statement

The switch statement is used to perform different actions based on different conditions. It’s an alternative to using multiple ‘elseif‘ statements and can make the code more readable.

Syntax:

				
					switch (n) {
    case value1:
        // Code to be executed if n=value1
        break;
    case value2:
        // Code to be executed if n=value2
        break;
    // You can have any number of case statements
    default:
        // Code to be executed if n doesn't match any case
}

				
			

Example:

				
					$day = "Wednesday";
switch ($day) {
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    case "Wednesday":
        echo "Today is Wednesday.";
        break;
    default:
        echo "Today is not Monday, Tuesday, or Wednesday.";
}

				
			

Detailed Example with Nested Conditions

Sometimes, you may need to nest conditions within each other to perform more complex checks.

Example:

				
					$day = "Wednesday";
switch ($day) {
    case "Monday":
        echo "Today is Monday.";
        break;
    case "Tuesday":
        echo "Today is Tuesday.";
        break;
    case "Wednesday":
        echo "Today is Wednesday.";
        break;
    default:
        echo "Today is not Monday, Tuesday, or Wednesday.";
}

				
			

n this example, the outer ‘if‘ statement checks if the age is 18 or above, and the nested ‘if‘ statements check the gender.

Key Points

  • Condition: Can be any expression that returns a boolean value (true or false).
  • Break Statement: Used inside switch to terminate the current case and prevent fall-through to the next case.
  • Default Case: Optional; executed if none of the cases match.

These conditional statements are essential for controlling the flow of a PHP program, making decisions, and executing specific blocks of code based on those decisions.

Scroll to Top