Conditional statements are a fundamental concept in programming that allow you to execute different blocks of code based on certain conditions. In PHP, the primary conditional statements are if, else, elseif, and switch.

  1. The if Statement

The if statement is used to execute a block of code only 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.";
}

Explanation:

  • The condition $age >= 18 is evaluated.
  • If the condition is true, the code inside the if block is executed, printing "You are an adult."

  1. The else Statement

The else statement is used to execute 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.";
}

Explanation:

  • If $age >= 18 is false, the code inside the else block is executed, printing "You are a minor."

  1. The elseif Statement

The elseif statement is used to specify a new condition to test if the first condition is false.

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 both condition1 and condition2 are false
}

Example:

$score = 75;

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

Explanation:

  • The conditions are checked sequentially.
  • If $score >= 90 is false, it checks $score >= 80, and so on.
  • The first true condition's block is executed, printing "Grade: C" for a score of 75.

  1. The switch Statement

The switch statement is used to perform different actions based on different conditions. It is often used when you have multiple conditions that depend on the same variable.

Syntax:

switch (n) {
    case value1:
        // code to be executed if n=value1
        break;
    case value2:
        // code to be executed if n=value2
        break;
    default:
        // code to be executed if n is different from all values
}

Example:

$day = "Tuesday";

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.";
}

Explanation:

  • The value of $day is compared against each case.
  • If $day matches "Tuesday", the corresponding block is executed, printing "Today is Tuesday."
  • The break statement prevents the code from running into the next case.

Practical Exercises

Exercise 1:

Write a PHP script that checks if a number is positive, negative, or zero.

Solution:

$number = -5;

if ($number > 0) {
    echo "The number is positive.";
} elseif ($number < 0) {
    echo "The number is negative.";
} else {
    echo "The number is zero.";
}

Exercise 2:

Create a PHP script that assigns a grade based on a student's score using a switch statement.

Solution:

$score = 85;

switch (true) {
    case ($score >= 90):
        echo "Grade: A";
        break;
    case ($score >= 80):
        echo "Grade: B";
        break;
    case ($score >= 70):
        echo "Grade: C";
        break;
    case ($score >= 60):
        echo "Grade: D";
        break;
    default:
        echo "Grade: F";
}

Common Mistakes and Tips:

  • Forgetting the break statement in switch cases: This can cause the code to fall through to the next case.
  • Not using curly braces {} in if statements: Always use curly braces to avoid logical errors, even for single-line statements.
  • Using = instead of == for comparison: Remember that = is for assignment, while == is for comparison.

Conclusion

In this section, you learned about the different types of conditional statements in PHP, including if, else, elseif, and switch. These statements allow you to control the flow of your program based on different conditions. Practice using these statements to become more comfortable with decision-making in your PHP scripts. Next, we will explore loops in PHP, which are essential for executing repetitive tasks.

PHP Programming Course

Module 1: Introduction to PHP

Module 2: Control Structures

Module 3: Functions

Module 4: Arrays

Module 5: Working with Forms

Module 6: Working with Files

Module 7: Object-Oriented Programming (OOP)

Module 8: Working with Databases

Module 9: Advanced PHP Techniques

Module 10: PHP Frameworks and Best Practices

Module 11: Project: Building a Web Application

© Copyright 2024. All rights reserved