<HTML ><HEAD ><TITLE >switch</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.7"><LINK REL="HOME" TITLE="PHP Manual" HREF="index.html"><LINK REL="UP" TITLE="Control Structures" HREF="control-structures.html"><LINK REL="PREVIOUS" TITLE="continue" HREF="control-structures.continue.html"><LINK REL="NEXT" TITLE="declare" HREF="control-structures.declare.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=ISO-8859-1"></HEAD ><BODY CLASS="sect1" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP Manual</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="control-structures.continue.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" >Chapter 12. Control Structures</TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="control-structures.declare.html" ACCESSKEY="N" >Next</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><DIV CLASS="sect1" ><H1 CLASS="sect1" ><A NAME="control-structures.switch" ></A ><TT CLASS="literal" >switch</TT ></H1 ><P > The <TT CLASS="literal" >switch</TT > 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 <TT CLASS="literal" >switch</TT > statement is for. </P ><P > The following two examples are two different ways to write the same thing, one using a series of <TT CLASS="literal" >if</TT > statements, and the other using the <TT CLASS="literal" >switch</TT > statement: <DIV CLASS="informalexample" ><A NAME="AEN5025" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >if ($i == 0) { print "i equals 0"; } if ($i == 1) { print "i equals 1"; } if ($i == 2) { print "i equals 2"; } switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > It is important to understand how the <TT CLASS="literal" >switch</TT > statement is executed in order to avoid mistakes. The <TT CLASS="literal" >switch</TT > statement executes line by line (actually, statement by statement). In the beginning, no code is executed. Only when a <TT CLASS="literal" >case</TT > statement is found with a value that matches the value of the <TT CLASS="literal" >switch</TT > expression does PHP begin to execute the statements. PHP continues to execute the statements until the end of the <TT CLASS="literal" >switch</TT > block, or the first time it sees a <TT CLASS="literal" >break</TT > statement. If you don't write a <TT CLASS="literal" >break</TT > statement at the end of a case's statement list, PHP will go on executing the statements of the following case. For example: <DIV CLASS="informalexample" ><A NAME="AEN5035" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >switch ($i) { case 0: print "i equals 0"; case 1: print "i equals 1"; case 2: print "i equals 2"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > Here, if $i is equal to 0, PHP would execute all of the print statements! If $i is equal to 1, PHP would execute the last two print statements. You would get the expected behavior ('i equals 2' would be displayed) only if $i is equal to 2. Thus, it is important not to forget <TT CLASS="literal" >break</TT > statements (even though you may want to avoid supplying them on purpose under certain circumstances). </P ><P > In a <TT CLASS="literal" >switch</TT > statement, the condition is evaluated only once and the result is compared to each <TT CLASS="literal" >case</TT > statement. In an <TT CLASS="literal" >elseif</TT > statement, the condition is evaluated again. If your condition is more complicated than a simple compare and/or is in a tight loop, a <TT CLASS="literal" >switch</TT > may be faster. </P ><P > The statement list for a case can also be empty, which simply passes control into the statement list for the next case. <DIV CLASS="informalexample" ><A NAME="AEN5045" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >switch ($i) { case 0: case 1: case 2: print "i is less than 3 but not negative"; break; case 3: print "i is 3"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > A special case is the default case. This case matches anything that wasn't matched by the other cases, and should be the last <TT CLASS="literal" >case</TT > statement. For example: <DIV CLASS="informalexample" ><A NAME="AEN5049" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >switch ($i) { case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; }</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ><P > The <TT CLASS="literal" >case</TT > 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. </P ><P > The alternative syntax for control structures is supported with switches. For more information, see <A HREF="control-structures.alternative-syntax.html" >Alternative syntax for control structures</A > . <DIV CLASS="informalexample" ><A NAME="AEN5055" ></A ><P ></P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><PRE CLASS="php" >switch ($i): case 0: print "i equals 0"; break; case 1: print "i equals 1"; break; case 2: print "i equals 2"; break; default: print "i is not equal to 0, 1 or 2"; endswitch;</PRE ></TD ></TR ></TABLE ><P ></P ></DIV > </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="control-structures.continue.html" ACCESSKEY="P" >Prev</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >Home</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="control-structures.declare.html" ACCESSKEY="N" >Next</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><TT CLASS="literal" >continue</TT ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="control-structures.html" ACCESSKEY="U" >Up</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><TT CLASS="literal" >declare</TT ></TD ></TR ></TABLE ></DIV ></BODY ></HTML >