SoFunction
Updated on 2025-03-02

Detailed explanation of the usage of switch statements in php

This article introduces the usage of switch statements in php. It is similar to switch usage in other statements, but note that there is a break statement.

Standard syntax for switch statements in PHP:

switch (expression)
{
case label1:
 code to be executed if expression = label1;
 break; 
case label2:
 code to be executed if expression = label2;
 break;
default:
 code to be executed
 if expression is different from both label1 and label2;
}

For example:

switch($i){
  case 1:
    echo 1;
    break;
  case 2:
    echo 2;
    break;
  default:
    echo 'others';
}

You can also use switch to determine a value range, or customize the conditions in the case.

<?php
header("content-type:text/html;charset=utf8");
$score=50;
switch($score) 
{
 case $score>=90 && $score<=100:
  echo "Excellent<br>";
  break;
 case $score&gt;=80 &amp;&amp; $score&lt;90:
  echo "Good<br>";
  break;
 case $score&gt;=70 &amp;&amp; $score&lt;80:
  echo "In the meantime<br>";
  break;
 case $score&gt;=60 &amp;&amp; $score&lt;70:
  echo "Pass<br>";
  break;
 case $score&gt;=0 &amp;&amp; $score&lt;60:
  echo "Failed<br>";
  break;
 default:
  echo"Score entry error<br>";
}
?&gt;

Simple example

&lt;?php 
 //Switch details //Scenario 1: When the numerical value matches, it will automatically convert into strings. $a=1; 
 switch($a){ 
  case "1": 
    echo 'hello1'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 2: Automatically convert into characters when the numerical values ​​match $a=1; 
 switch($a){ 
  case '1': 
    echo 'hello2'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 3: Automatically convert to numeric values ​​when characters match $a='1'; 
 switch($a){ 
  case 1: 
    echo 'hello3'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 4: Automatically convert to numerical values ​​when string matches $a="1"; 
 switch($a){ 
  case 1: 
    echo 'hello4'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 5: float can also match $a=1.1; 
 switch($a){ 
  case 1.1: 
    echo 'hello5'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 6: Non-0 is true $a=true; 
 switch($a){ 
  case 1: 
    echo 'hello6'; 
    break; 
  case true: 
    echo 'hello61'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 7: Can match boolean $a=true; 
 switch($a){ 
  case true: 
    echo 'hello7'; 
    break; 
  case 2: 
    echo 'hello71'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 8: Can match null $a=null; 
 switch($a){ 
  case 2://'' "" false 0 can enter    echo 'hello8'; 
    break; 
  case null: 
    echo 'hello81'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 9: Exit order $a=5; 
 switch($a){ 
  case 1: 
    echo 'hello6'; 
    break; 
  case 2: 
    echo 'I'm Exit 2'; 
    break; 
  case 5: 
    echo 'I'm Exit 5'; 
    //break; 
  case true: 
    echo 'hello61'; 
    break; 
  default: 
    echo 'sorry none is the same!'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
  //Scenario 10: Exit order $a=50; 
 switch($a){ 
  default: 
    echo 'sorry none is the same!'; 
    //break; 
  case 50: 
    echo 'hello6'; 
    //break; 
  case 2: 
    echo 'I'm Exit 2'; 
    break; 
  case 5: 
    echo 'I'm Exit 5'; 
    //break; 
  case 6: 
    echo 'hello61'; 
    break; 
 } 
 echo '&lt;br/&gt;'.'Exit successfully...'; 
 echo '&lt;hr/&gt;'; 
?&gt; 

Running results

hello1
Exit successfully····
hello2
Exit successfully····
hello3
Exit successfully····
hello4
Exit successfully····
hello5
Exit successfully····
hello6
Exit successfully····
hello7
Exit successfully····
hello81
Exit successfully····
I am5Exit numberhello61
Exit successfully····
hello6I am2Exit number
Exit successfully····