PHP Basics: Switch Statement

PHP Basics: Switch Statement


2 min read

In PHP, the switch statement is used to execute different code based on the value of a given expression. It is similar to the if statement, but it is often used when you have multiple conditions that need to be checked and you want to use a more concise syntax. Here is an example of how it works:

$fruit = "apple";

switch ($fruit) {
  case "apple":
    echo "You chose an apple!";
  case "banana":
    echo "You chose a banana!";
  case "orange":
    echo "You chose an orange!";
    echo "You chose something else!";

In this example, the value of the $fruit variable is checked against each of the case statements inside the switch block. If the value of $fruit matches the value of a case statement, the code inside that case block will be executed. In this case, if $fruit is equal to "apple", the code inside the first case block will be executed and the message "You chose an apple!" will be printed. If the value of $fruit does not match any of the case statements, the code inside the default block will be executed.

The break statement is used inside each case block to indicate that the switch statement should stop executing once that case block has been executed. If you omit the break statement, the code from the next case block will also be executed, even if the value of the expression does not match the case label. This can be useful in some cases, but it can also lead to unexpected behavior if you are not careful.

Did you find this article valuable?

Support 0xTristan by becoming a sponsor. Any amount is appreciated!