★ wanayoo — archive 1999 http://www.php.net/manual/control-structures.switch.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Control Structures
Quick Reference
Control Structures
* if
* else
* elseif
* Alternative syntax for control structures
* while
* do..while
* for
* foreach
* break
* continue
* switch
* require
* include
Manual: switch
View the source code for this pageSearch the site



Previous page
 continue
require 
Next page


switch

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.

The following two examples are two different ways to write the same thing, one using a series of if statements, and the other using the switch statement:

  1 
  2 if ($i == 0) {
  3     print "i equals 0";
  4 }
  5 if ($i == 1) {
  6     print "i equals 1";
  7 }
  8 if ($i == 2) {
  9     print "i equals 2";
 10 }
 11  
 12 switch ($i) {
 13     case 0:
 14         print "i equals 0";
 15         break;
 16     case 1:
 17         print "i equals 1";
 18         break;
 19     case 2:
 20         print "i equals 2";
 21         break;
 22 }
 23      

It is important to understand how the switch statement is executed in order to avoid mistakes. The switch statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a case statement is found with a value that matches the value of the switch expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the switch block, or the first time it sees a break statement. If you don't write a break statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example:

  1 
  2 switch ($i) {
  3     case 0:
  4         print "i equals 0";
  5     case 1:
  6         print "i equals 1";
  7     case 2:
  8         print "i equals 2";
  9 }
 10      

Here, if $i equals to 0, PHP would execute all of the print statements! If $i equals to 1, PHP would execute the last two print statements, and only if $i equals to 2, you'd get the 'expected' behavior and only 'i equals 2' would be displayed. So, it's important not to forget break statements (even though you may want to avoid supplying them on purpose under certain circumstances).

In a switch statement, the condition is evaluated only once and the result is compared to each case statement. In an elseif statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a switch may be faster.

The statement list for a case can also be empty, which simply passes control into the statement list for the next case.

  1 
  2 switch ($i) {
  3     case 0:
  4     case 1:
  5     case 2:
  6         print "i is less than 3 but not negative";
  7         break;
  8     case 3:
  9         print "i is 3";
 10 }
 11      

A special case is the default case. This case matches anything that wasn't matched by the other cases. For example:

  1 
  2 switch ($i) {
  3     case 0:
  4         print "i equals 0";
  5         break;
  6     case 1:
  7         print "i equals 1";
  8         break;
  9     case 2:
 10         print "i equals 2";
 11         break;
 12     default:
 13         print "i is not equal to 0, 1 or 2";
 14 }
 15      

The case expression may be any expression that evaluates to a simple type, that is, integer or floating-point numbers and strings. Arrays or objects cannot be used here unless they are dereferenced to a simple type.

The alternative syntax for control structures is supported with switches. For more information, see Alternative syntax for control structures .

  1 
  2 switch ($i):
  3     case 0:
  4         print "i equals 0";
  5         break;
  6     case 1:
  7         print "i equals 1";
  8         break;
  9     case 2:
 10         print "i equals 2";
 11         break;
 12     default:
 13         print "i is not equal to 0, 1 or 2";
 14 endswitch;
 15      


User Contributed Notes: switch


bpeace@mindspring.com
22-Dec-1999 07:52
Just a note. The default case must be the last case.


garrett@yavin.org
07-Jan-2000 07:25
Unlike C, you can use strings in case statements, not just integers. E.g.:
switch ($x) {
case "yes":
	// do something
	break;

case "no":
	// do something else
	break;

case "maybe":
	// do something else
	break;

default:
	// user didn't pick a reasonable option
	die("illegal choice");
}
... case "


kcavness@proxicom.com
05-Feb-2000 01:41
You can _only_ use integer ordinals in C (and, as far as I know, C++) in a switch statement. Thanks, authors of PHP, for including that. You've actually made switch() _worth_ something... now if only someone would implement ranges for switch in PHP (i.e. vb-or-Pascal/Delphi style "1..5:")


justin@dolphintech.net
08-Apr-2000 01:56
By adding switch capabilities on strings, they have potentially lost the speed increase gained by using a switch on constants only, because what C/C++ does is create a jump table for the switch statement to increase speed. It is possible that these string switch statements are actually being broken down into more expansive switch statements in C/C++, which will retain the speed of the switch as well as give support for strings, which in this sense can be done in C/C++ already.


 About Notes


Previous page
 continue
require 
Next page



Site Statistics


Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.