PHP Switch Statements

Why Not “Switch” from If/Else?

 

switch ( $arg ) {
  case "value_1":
      expression if matched value_1
      break; // terminate
  case "value_2":
      expression if matched value_2
      break; // terminate
  ...
  default:
      execute if no cases matched
}

 

 

<?php

$name = "Dino";

if ( $name == "Dino" ) {
    echo "Hey man!";
} elseif ( $name == "Harrison" ) {
    echo "How's it going?";
} elseif ( $name == "Amy" ) {
    echo "Hello";
} else {
    echo "Hi";
}

?>

 

The if/elseif/else statement above looks at the $name variable. Depending on which condition matches, a specific greeting is displayed. If the $name variable contains the value “Harrison,” PHP will output “How’s it going?” If no matches are found, PHP will output “Hi.” Let’s convert this into a switch statement.

 

<?php

$name = "Dino";

switch ( $name ) {
  case "Dino":
    echo "Hey man";
    break;
  case "Harrison":
    echo "How's it going?";
    break;
  case "Amy":
    echo "Hello";
    break;
  default:
    echo "Hi";
}

?>

 

As you can see, the code is a lot more cleaner to look at than the if/elseif/else statement. In the example above, PHP will output “Hey man” since “Dino” will be matched. It will not continue execution due to the break statement. If there were several dozen options, you could see how a switch statement could be beneficial. We can even make it more compact; we don’t have to place each statement on a new line.

 

<?php

$name = "Dino";

switch ( $name ) {
  case "Dino": echo "Hey man"; break;
  case "Harrison": echo "How's it going?"; break;
  case "Amy": echo "Hello"; break;
  default: echo "Hi";
}

?>

 

 

<?php

$money_in_the_bank = 40000;
$car = "";

switch( $money_in_the_bank ) {
    case 1000000: $car = "McLaren F1";  break;
    case 500000:  $car = "Murcielago";  break;
    case 100000:  $car = "Porsche 911"; break;
    case 75000:   $car = "Hellcat";     break;
    case 40000:   $car = "Subaru STi";  break;
    case 25000:   $car = "Civic";       break;
}

var_dump( $car );

?>

 

 

<?php

$money_in_the_bank2 = 1000000;

switch( $money_in_the_bank2 ) {
    case 1000000: $car = "McLaren F1";
    case 500000:  $car = "Murcielago";
    case 100000:  $car = "Porsche 911";
    case 75000:   $car = "Hellcat";
    case 40000:   $car = "Subaru STi";
    case 25000:   $car = "Civic";
}

var_dump($car);

?>

 

 

<?php

$brand = "Chevy";

switch( $brand ) {
    case "Apple":
    case "HP":
    case "Samsung":
        echo "This is a tech company";
        break;
    case "Audi":
    case "Chevy":
    case "Porsche":
    case "Subaru":
        echo "This is a car company";
        break;
    default:
        echo "I don't know what this is.";
}

?>

 

 

<?php

$grade = 80;

// A = 90-100, B = 80-89, C = 70-79, D = 60-69, F = 0-59
switch( $grade ) {
    case ($grade >= 90): echo "A"; break;
    case ($grade >= 80): echo "B"; break;
    case ($grade >= 70): echo "C"; break;
    case ($grade >= 60): echo "D"; break;
    default: echo "F";
}

?>

 

The order is important. We start with the highest grade and work our way down. PHP will output “B.” If the code was reversed, PHP would output “D” since $grade ≥ 60 is a true statement.

 

 

PHP Tutorial Series

Continue your learning with these articles

PHP Ternary Operator

tired of if/else? How about ternary?

PHP – P27: Ternary Operator

The ternary operator is a way to quickly express if/else statements. If the boolean expression evaluates to true, the “if_true” result is displayed, otherwise the “if_false” result is displayed.

PHP Switch Statements

Why Not “Switch” from If/Else?

PHP – P28: Switch Statement

Are you tired of writing if/elseif/elseif/…/else statements? You’re not alone. There is a more elegant way to write such large statements: the switch statement.

PHP while loops

THIS MIGHT TAKE A WHILE

PHP – P29: WHILE LOOPS

The while loop says, “while the expression is true, do something inside of the loop body.” If we passed the value “true” to the conditional expression, the loop would run forever (well, as long as PHP allows it to run before it crashes).

Leave a Reply