switch

(PHP 4, PHP 5, PHP 7, PHP 8)

switch 语句类似于具有同一个表达式的一系列 if 语句。很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码。这正是 switch 语句的用途。

注意: 注意和其它语言不同,continue 语句作用到 switch 上的作用类似于 break。如果在循环中有一个 switch 并希望 continue 到外层循环中的下一轮循环,用 continue 2

注意:

注意 switch/case 作的是松散比较

下面的例子使用不同方法实现同样的事。一个用一系列的 ifelseif 语句,另一个用 switch 语句。例子不同,但输出相同:

示例 #1 switch 结构

// 这是 switch 语句

switch ($i) {
case 0:
echo "i equals 0";
break;
case 1:
echo "i equals 1";
break;
case 2:
echo "i equals 2";
break;
}

// 相当于:

if ($i == 0) {
echo "i equals 0";
} elseif ($i == 1) {
echo "i equals 1";
} elseif ($i == 2) {
echo "i equals 2";
?>

为避免错误,理解 switch 是怎样执行的非常重要。switch 语句一行接一行地执行(实际上是语句接语句)。开始时没有代码被执行。仅当一个 case 语句中的值和 switch 表达式的值匹配时 PHP 才开始执行语句,直到 switch 的程序段结束或者遇到第一个 break 语句为止。如果不在 case 的语句段最后写上 break 的话,PHP 将继续执行下一个 case 中的语句段。例如:

<?php
switch ($i) {
case
0:
echo
"i equals 0";
case
1:
echo
"i equals 1";
case
2:
echo
"i equals 2";
}
?>

这里如果 $i 等于 0,PHP 将执行所有的 echo 语句!如果 $i 等于 1,PHP 将执行后面两条 echo 语句。只有当 $i 等于 2 时,才会得到“预期”的结果——只显示“i equals 2”。所以,别忘了 break 语句就很重要(即使在某些情况下故意想避免提供它们时)。

switch 语句中条件只求值一次并用来和每个 case 语句比较。在 elseif 语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中,那么用 switch 语句可能会快一些。

在一个 case 中的语句也可以为空,这样只不过将控制转移到了下一个 case 中的语句。

<?php
switch ($i) {
case
0:
case
1:
case
2:
echo
"i is less than 3 but not negative";
break;
case
3:
echo
"i is 3";
}
?>

一个 case 的特例是 default。它匹配了任何和其它 case 都不匹配的情况。例如:

<?php
switch ($i) {
case
0:
echo
"i equals 0";
break;
case
1:
echo
"i equals 1";
break;
case
2:
echo
"i equals 2";
break;
default:
echo
"i is not equal to 0, 1 or 2";
}
?>

注意: 如果有多个 default 将导致 E_COMPILE_ERROR 错误。

注意: 从技术上讲,default case 可以按照任何顺序列出。只有在没有匹配到其它的 case 时才会使用它。但是最好按照惯例,将其作为最后一个分支放在最后。

如果没有匹配到 case 分支且没有 default 分支,则不会执行任何代码,就像 if 不为 true 一样。

case 的值可以使用表达式。然而,该表达式将会自我求值,然后与 switch 的值进行松散比较。这意味着它不适合用于复杂的 switch 值求值。例如:

<?php
$target
= 1;
$start = 3;

switch (
$target) {
case
$start - 1:
print
"A";
break;
case
$start - 2:
print
"B";
break;
case
$start - 3:
print
"C";
break;
case
$start - 4:
print
"D";
break;
}

// 输出“B”
?>

对于更复杂的比较,值 true 可用于 switch 的值。或使用 if-else 代替 switch

<?php
$offset
= 1;
$start = 3;

switch (
true) {
case
$start - $offset === 1:
print
"A";
break;
case
$start - $offset === 2:
print
"B";
break;
case
$start - $offset === 3:
print
"C";
break;
case
$start - $offset === 4:
print
"D";
break;
}

// 输出“B”
?>

switch 支持替代语法的流程控制。更多信息见流程控制的替代语法一节。

<?php
switch ($i):
case
0:
echo
"i equals 0";
break;
case
1:
echo
"i equals 1";
break;
case
2:
echo
"i equals 2";
break;
default:
echo
"i is not equal to 0, 1 or 2";
endswitch;
?>

允许使用分号代替 case 语句后的冒号,例如:

<?php
switch($beer)
{
case
'tuborg';
case
'carlsberg';
case
'stella';
case
'heineken';
echo
'Good choice';
break;
default;
echo
'Please make a new selection...';
break;
}
?>

参见

add a note add a note

User Contributed Notes 6 notes

up
287
MaxTheDragon at home dot nl
12 years ago
This is listed in the documentation above, but it's a bit tucked away between the paragraphs. The difference between a series of if statements and the switch statement is that the expression you're comparing with, is evaluated only once in a switch statement. I think this fact needs a little bit more attention, so here's an example:

<?php
$a
= 0;

if(++
$a == 3) echo 3;
elseif(++
$a == 2) echo 2;
elseif(++
$a == 1) echo 1;
else echo
"No match!";

// Outputs: 2

$a = 0;

switch(++
$a) {
    case
3: echo 3; break;
    case
2: echo 2; break;
    case
1: echo 1; break;
    default: echo
"No match!"; break;
}

// Outputs: 1
?>

It is therefore perfectly safe to do:

<?php
switch(winNobelPrizeStartingFromBirth()) {
case
"peace": echo "You won the Nobel Peace Prize!"; break;
case
"physics": echo "You won the Nobel Prize in Physics!"; break;
case
"chemistry": echo "You won the Nobel Prize in Chemistry!"; break;
case
"medicine": echo "You won the Nobel Prize in Medicine!"; break;
case
"literature": echo "You won the Nobel Prize in Literature!"; break;
default: echo
"You bought a rusty iron medal from a shady guy who insists it's a Nobel Prize..."; break;
}
?>

without having to worry about the function being re-evaluated for every case. There's no need to preemptively save the result in a variable either.
up
115
septerrianin at mail dot ru
6 years ago
php 7.2.8.
The answer to the eternal question " what is faster?":
1 000 000 000 iterations.

<?php
$s
= time();
for (
$i = 0; $i < 1000000000; ++$i) {
 
$x = $i%10;
  if (
$x == 1) {
   
$y = $x * 1;
  } elseif (
$x == 2) {
   
$y = $x * 2;
  } elseif (
$x == 3) {
   
$y = $x * 3;
  } elseif (
$x == 4) {
   
$y = $x * 4;
  } elseif (
$x == 5) {
   
$y = $x * 5;
  } elseif (
$x == 6) {
   
$y = $x * 6;
  } elseif (
$x == 7) {
   
$y = $x * 7;
  } elseif (
$x == 8) {
   
$y = $x * 8;
  } elseif (
$x == 9) {
   
$y = $x * 9;
  } else {
   
$y = $x * 10;
  }
}
print(
"if: ".(time() - $s)."sec\n");

$s = time();
for (
$i = 0; $i < 1000000000; ++$i) {
 
$x = $i%10;
  switch (
$x) {
  case
1:
   
$y = $x * 1;
    break;
  case
2:
   
$y = $x * 2;
    break;
  case
3:
   
$y = $x * 3;
    break;
  case
4:
   
$y = $x * 4;
    break;
  case
5:
   
$y = $x * 5;
    break;
  case
6:
   
$y = $x * 6;
    break;
  case
7:
   
$y = $x * 7;
    break;
  case
8:
   
$y = $x * 8;
    break;
  case
9:
   
$y = $x * 9;
    break;
  default:
   
$y = $x * 10;
  }
}
print(
"switch: ".(time() - $s)."sec\n");
?>

Results:
if: 69sec
switch: 42sec
up
76
nospam at please dot com
24 years ago
Just a trick I have picked up:

If you need to evaluate several variables to find the first one with an actual value, TRUE for instance. You can do it this was.

There is probably a better way but it has worked out well for me.

switch (true) {

  case (X != 1):

  case (Y != 1):

  default:
}
up
3
me at czarpino dot com
2 years ago
Although noted elsewhere, still worth noting is how loose comparison in switch-case was also affected by the change in string to number comparison. Prior PHP8, strings were converted to int before comparison. The reverse is now true which can cause issues for logic that relied on this behavior.

<?php
function testSwitch($key) {
    switch (
$key) {
        case
'non numeric string':
            echo
$key . ' matches "non numeric string"';
            break;
    }
}

testSwitch(0);    // pre-PHP8, returns '0 matches "non numeric string"'
?>
up
2
j dot kane dot third at gmail dot com
2 years ago
The default case appears to always be evaluated last. If break is excluded from the default case, then the proceeding cases will be reevaluated. This behavior appears to be undocumented.

<?php

$kinds
= ['moo', 'kind1', 'kind2'];

foreach (
$kinds as $kind) {
    switch(
$kind)
    {
        default:
           
// The kind wasn't valid, set it to the default
           
$kind = 'kind1';
           
var_dump('default');
   
        case
'kind1':
           
var_dump('1');
            break;
   
        case
'kind2':
           
var_dump('2');
            break;
   
        case
'kindn':
           
var_dump('n-th');
            break;
    }
   
    echo
"\n\n";
}

?>
up
-2
GeorgNation
1 year ago
You can wrap up the case/break block with a curly braces:

$x = 2;

switch ($x)
{
    case 2: {
        echo '2 entrypoint';
        break;
    }
   
    default: {
        echo 'default entrypoint';
        break;
    }
}
To Top