Curriculum
Course: PHP Basic
Login

Curriculum

PHP Basic

PHP Install

0/1

PHP Casting

0/1

PHP Constants

0/1

PHP Magic Constants

0/1

PHP Operators

0/1

PHP Reference

0/276
Text lesson

PHP switch

The PHP switch Statement

Use the switch statement to choose one of several blocks of code to execute.

Syntax

switch (expression) {
  case label1:
    //code block
    break;
  case label2:
    //code block;
    break;
  case label3:
    //code block
    break;
  default:
    //code block
}

Here’s how it works:

  1. The expression is evaluated once.
  2. The value of the expression is compared with each case’s value.
  3. If a match is found, the corresponding block of code is executed.
  4. The break keyword exits the switch block.
  5. The default block is executed if no match is found.

Example

$favcolor = "red";

switch
($favcolor) {
 case "red":
    echo "Your favorite color is red!";
    break;
 case "blue":
    echo "Your favorite color is blue!";
    break;
case "green":
    echo "Your favorite color is green!";
    break;
 default:
    echo "Your favorite color is neither red, blue, nor green!";
}

The break Keyword

When PHP encounters a break keyword, it exits the switch block, stopping the execution of any additional code, and no further cases are evaluated.

The last block does not require a break, as the block naturally ends there.

Warning: If you omit the break statement in a case that is not the last, and that case matches, the next case will also be executed, regardless of whether it matches!

Example

What happens if we remove the break statement from case “red”?

Since $favcolor is red, the code block for case “red” will execute, but without a break statement, the code block for case “blue” will also execute.

$favcolor = "red";
switch ($favcolor) {
 case "red":
    echo "Your favorite color is red!";
 case "blue":
    "Your favorite color is blue!";
    break;
 case "green":
    echo "Your favorite color is green!";
    break;
 default:
    echo "Your favorite color is neither red, blue, nor green!";
}

The default Keyword

The default keyword indicates the code to execute if no case matches.

Example

If none of the cases match, the default block is executed.

$d = 4;
switch ($d) {
 case 6:
    echo "Today is Saturday";
    break;
 case 0:
    echo "Today is Sunday";
    break;
 default:
    echo "Looking forward to the Weekend";
}

The default case does not need to be the final case in a switch block.

Example

Placing the default block anywhere other than at the end of the switch block is permitted, but not advisable.

$d = 4;
switch ($d) {
 default:
    echo "Looking forward to the Weekend";
    break;
 case 6:
    echo "Today is Saturday";
    break;
 case 0:
    echo "Today is Sunday";
}
Note: If the default block is not the last block in the switch statement, be sure to end it with a break statement.

 

Common Code Blocks

If you want multiple cases to execute the same code block, you can specify the cases as follows:

Example

Multiple cases for each code block:

$d = 3;
switch ($d) {
 case 1:
 case 2:
 case 3:
 case 4:
 case 5: 
    echo "The weeks feels so long!";
    break;
 case 6:
 case 0:
    echo "Weekends are the best!";
    break;
 default:
    echo "Something went wrong";
}