Using the ‘case’ keyword in PHP switch statements

The case keyword in PHP is used within a switch statement to specify a particular condition to test against. It is used to compare the value of a variable or expression with a set of values and execute a block of code when a match is found. The case keyword is often used to execute different code blocks based on the value of a variable, or to execute the same code block for multiple values. Each case statement must end with a break statement to exit the switch statement once a matching case has been found. The default statement can be used to specify a code block to execute if none of the case statements match the value being tested.

Basic usage of the case keyword

Here is a basic example of how to use the case keyword in a PHP switch statement:

$value = 'a';

switch ($value) {
  case 'a':
    echo 'The value is a';
    break;
  case 'b':
    echo 'The value is b';
    break;
  default:
    echo 'The value is neither a nor b';
    break;
}

In this example, the value of the $value variable is tested against the values specified in each case statement. If the value of $value is 'a', the code block associated with the case 'a' statement will be executed. If the value of $value is 'b', the code block associated with the case 'b' statement will be executed. If the value of $value is neither 'a' nor 'b', the code block associated with the default statement will be executed.

It’s important to note that each case statement must end with a break statement. The break statement is used to exit the switch statement once a matching case has been found and its associated code block has been executed. If a break statement is not included, the code will continue to execute through all of the remaining case statements until a break statement is encountered or the end of the switch statement is reached.

Multiple case statements

Here is an example of how you can use multiple case statements to execute different code blocks based on different values of a variable:

$day = 'Monday';

switch ($day) {
  case 'Monday':
    echo 'Today is Monday';
    break;
  case 'Tuesday':
    echo 'Today is Tuesday';
    break;
  case 'Wednesday':
    echo 'Today is Wednesday';
    break;
  case 'Thursday':
    echo 'Today is Thursday';
    break;
  case 'Friday':
    echo 'Today is Friday';
    break;
  case 'Saturday':
    echo 'Today is Saturday';
    break;
  case 'Sunday':
    echo 'Today is Sunday';
    break;
  default:
    echo 'Invalid day';
    break;
}

In this example, the value of the $day variable is tested against the values specified in each case statement. If the value of $day is 'Monday', the code block associated with the case 'Monday' statement will be executed. If the value of $day is 'Tuesday', the code block associated with the case 'Tuesday' statement will be executed, and so on. If the value of $day is none of the values specified in the case statements, the code block associated with the default statement will be executed.

Comparing a variable or expression to a range of values

You can also use the case keyword to compare the value of a variable or expression to a range of values using the case x: and case y: syntax, like this:

$age = 30;

switch (true) {
  case ($age < 18):
    echo 'You are a minor';
    break;
  case ($age >= 18 && $age < 30):
    echo 'You are an adult';
    break;
  case ($age >= 30 && $age < 50):
    echo 'You are middle-aged';
    break;
  case ($age >= 50):
    echo 'You are a senior';
    break;
}

In this example, the switch statement is evaluating the value of true (which will always be true) and then comparing the value of the $age variable with a range of values in each case statement. If the value of $age is less than 18, the code block associated with the case ($age < 18) statement will be executed. If the value of $age is greater than or equal to 18 and less than 30, the code block associated with the case ($age >= 18 && $age < 30) statement will be executed, and so on.

Executing the same code block for multiple values

It’s also possible to use the case keyword to execute the same code block for multiple values by separating the values with commas, like this:

$fruit = 'apple';

switch ($fruit) {
  case 'apple', 'banana', 'orange':
    echo 'The fruit is a type of citrus';
    break;
  case 'grapes', 'strawberry':
    echo 'The fruit is a type of berry';
    break;
  default:
    echo 'The fruit is not a citrus or a berry';
    break;
}

In this example, the value of the $fruit variable is tested against the values specified in each case statement. If the value of $fruit is 'apple', 'banana', or 'orange', the code block associated with the case 'apple', 'banana', 'orange' statement will be executed. If the value of $fruit is 'grapes' or 'strawberry', the code block associated with the case 'grapes', 'strawberry' statement will be executed. If the value of $fruit is none of the values specified in the case statements, the code block associated with the default statement will be executed.

Conclusion

  • The case keyword is used within a switch statement to specify a particular condition to test against.
  • It is used to compare the value of a variable or expression with a set of values and execute a block of code when a match is found.
  • The case keyword can be used to execute different code blocks based on the value of a variable, or to execute the same code block for multiple values.
  • Each case statement must end with a break statement to exit the switch statement once a matching case has been found.
  • The default statement can be used to specify a code block to execute if none of the case statements match the value being tested.
  • The case keyword is a useful tool for creating conditional statements in PHP, and can improve the readability and maintainability of code by allowing multiple conditions to be tested in a single switch statement.