The switch Statement

The switch statement compares an expression to numerous values.

The switch statement works by taking the value after the switch keyword and comparing it to the cases in the order in which they appear. In no case matches, the code isn't executed. Once a case matches, the code is executed. The code in subsequent cases also executes until the end of the switch statement or until a break keyword.

switch (expression) {
  case "label1":
    code to be executed if expression = label1;
  case "label2":
    code to be executed if expression = label2;
  case "label3":
    code to be executed if expression = label3;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$wheather ="sunny";
switch ($wheather) {
  case "sunny":
   echo "Well I like a $wheather day.<br />";
  case "bad":
   echo "Well today is not a $wheather day.<br />";
}

$temperature = 70;
switch ($temperature) {
  case "70":
   echo "It's getting warm.<br />";
  case "90":
   echo "It's hot.<br />";
}
?>
</p>
</body>
</html>

View the effect

When PHP comes across the break keyword, processing jumps to the next line after the entire switch statement.

switch (expression) {
  case "label1":
    code to be executed if expression = label1;
    break;
  case "label2":
    code to be executed if expression = label2;
    break;
  case "label3":
    code to be executed if expression = label3;
    break;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$wheather ="sunny";
switch ($wheather) {
  case "sunny":
   echo "Well I like a $wheather day.<br />";
   break;
  case "bad":
   echo "Well today is not a $wheather day.<br />";
   break;
}

$temperature = 70;
switch ($temperature) {
  case "70":
   echo "It's getting warm.<br />";
   break;
  case "90":
   echo "It's hot.<br />";
   break;
}
?>
</p>
</body>
</html>

View the effect

The switch statement also provides a way to do something if none of the other cases matches. Use the default: statement.

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 from both label1 and label2;
}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="EN" lang="EN">
<head>
<title>PHP</title>
<meta Name="Author" Content="Hann So">
</head>
<body>
<p>
<?php
$wheather ="cloudy";
switch ($wheather) {
  case "sunny":
   echo "Well I like a $wheather day.<br />";
   break;
  case "bad":
   echo "Well today is not a $wheather day.<br />";
   break;
  default:
   echo "Well today is a $wheather day.<br />";
}

$temperature = 80;
switch ($temperature) {
  case "70":
   echo "It's getting warm.<br />";
   break;
  case "90":
   echo "It's hot.<br />";
   break;
  default:
   echo "$temperature degrees is a good day.<br />";
}
?>
</p>
</body>
</html>

View the effect


Conditional Tests | The if Statement | The else Statement | The elseif Statement | The switch Statement
© 2008: Hann So
email: hso@voyager.deanza.edu