Sophie

Sophie

distrib > Mandriva > 2008.1 > x86_64 > by-pkgid > 05cd670d8a02b2b4a0ffb1756f2e8308 > files > 29

php-manual-zh-5.2.4-1mdv2008.1.noarch.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>switch</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REL="HOME"
TITLE="PHP 手册"
HREF="index.html"><LINK
REL="UP"
TITLE="控制结构"
HREF="language.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=UTF-8"></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 手册</TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="bottom"
><A
HREF="control-structures.continue.html"
ACCESSKEY="P"
>上一页</A
></TD
><TD
WIDTH="80%"
ALIGN="center"
VALIGN="bottom"
>章 16. 控制结构</TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="bottom"
><A
HREF="control-structures.declare.html"
ACCESSKEY="N"
>下一页</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="sect1"
><H1
CLASS="sect1"
><A
NAME="control-structures.switch"
><TT
CLASS="literal"
>switch</TT
></A
></H1
><P
>&#13;   <TT
CLASS="literal"
>switch</TT
> 语句和具有同样表达式的一系列的 IF
   语句相似。很多场合下需要把同一个变量(或表达式)与很多不同的值比较,并根据它等于哪个值来执行不同的代码。这正是
   <TT
CLASS="literal"
>switch</TT
> 语句的用途。
  </P
><DIV
CLASS="note"
><BLOCKQUOTE
CLASS="note"
><P
><B
>注意: </B
>
    注意和其它语言不同,<A
HREF="control-structures.continue.html"
>continue</A
>
    语句作用到 switch 上的作用类似于 <TT
CLASS="literal"
>break</TT
>。如果在循环中有一个
    switch 并希望 continue 到外层循环中的下一个轮回,用 <TT
CLASS="literal"
>continue 2</TT
>。
   </P
></BLOCKQUOTE
></DIV
><P
>&#13;   下面两个例子使用两种不同方法实现同样的事,一个用一系列的
   <TT
CLASS="literal"
>if</TT
> 语句,另一个用 <TT
CLASS="literal"
>switch</TT
> 语句:
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN4927"
></A
><P
><B
>例 16-1. <TT
CLASS="literal"
>switch</TT
> 结构</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">if (</font><font color="#0000BB">$i </font><font color="#007700">== </font><font color="#0000BB">0</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 0"</font><font color="#007700">;<br />} elseif (</font><font color="#0000BB">$i </font><font color="#007700">== </font><font color="#0000BB">1</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 1"</font><font color="#007700">;<br />} elseif (</font><font color="#0000BB">$i </font><font color="#007700">== </font><font color="#0000BB">2</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 2"</font><font color="#007700">;<br />}<br /><br />switch (</font><font color="#0000BB">$i</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">0</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 0"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">1</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 1"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">2</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 2"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
   <TABLE
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
CLASS="EXAMPLE"
><TR
><TD
><DIV
CLASS="example"
><A
NAME="AEN4931"
></A
><P
><B
>例 16-2. <TT
CLASS="literal"
>switch</TT
> 结构可以用字符串</B
></P
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">switch (</font><font color="#0000BB">$i</font><font color="#007700">) {<br />case </font><font color="#DD0000">"apple"</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is apple"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />case </font><font color="#DD0000">"bar"</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is bar"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />case </font><font color="#DD0000">"cake"</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is cake"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;break;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
></DIV
></TD
></TR
></TABLE
>
  </P
><P
>&#13;   为避免错误,理解 <TT
CLASS="literal"
>switch</TT
> 是怎样执行的非常重要。<TT
CLASS="literal"
>switch</TT
>
   语句一行接一行地执行(实际上是语句接语句)。开始时没有代码被执行。仅当一个
   <TT
CLASS="literal"
>case</TT
> 语句中的值和 <TT
CLASS="literal"
>switch</TT
>
   表达式的值匹配时 PHP 才开始执行语句,直到 <TT
CLASS="literal"
>switch</TT
>
   的程序段结束或者遇到第一个 <TT
CLASS="literal"
>break</TT
>
   语句为止。如果不在 case 的语句段最后写上 <TT
CLASS="literal"
>break</TT
>
   的话,PHP 将继续执行下一个 case 中的语句段。例如:
   <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN4943"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">switch (</font><font color="#0000BB">$i</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">0</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 0"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">1</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 1"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">2</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 2"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
  </P
><P
>&#13;   这里如果 <CODE
CLASS="varname"
>$i</CODE
> 等于 0,PHP 将执行所有的 print 语句!如果
   <CODE
CLASS="varname"
>$i</CODE
> 等于 1,PHP 将执行后面两条 print 语句。只有当
   <CODE
CLASS="varname"
>$i</CODE
> 等于 2 时,才会得到“预期”的结果――只显示“i equals 2”。所以,别忘了
   <TT
CLASS="literal"
>break</TT
> 语句就很重要(即使在某些情况下故意想避免提供它们时)。
  </P
><P
>&#13;   在 <TT
CLASS="literal"
>switch</TT
> 语句中条件只求值一次并用来和每个
   <TT
CLASS="literal"
>case</TT
> 语句比较。在 <TT
CLASS="literal"
>elseif</TT
>
   语句中条件会再次求值。如果条件比一个简单的比较要复杂得多或者在一个很多次的循环中,那么用
   <TT
CLASS="literal"
>switch</TT
> 语句可能会快一些。
  </P
><P
>&#13;   在一个 case 中的语句也可以为空,这样只不过将控制转移到了下一个 case 中的语句。
   <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN4956"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">switch (</font><font color="#0000BB">$i</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">0</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">1</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">2</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is less than 3 but not negative"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">3</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is 3"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
  </P
><P
>&#13;   一个 case 的特例是 <TT
CLASS="literal"
>default</TT
>。它匹配了任何和其它
   case 都不匹配的情况,并且应该是最后一条 <TT
CLASS="literal"
>case</TT
> 语句。例如:
   <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN4961"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">switch (</font><font color="#0000BB">$i</font><font color="#007700">) {<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">0</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 0"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">1</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 1"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">2</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 2"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is not equal to 0, 1 or 2"</font><font color="#007700">;<br />}<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></TD
></TR
></TABLE
><P
></P
></DIV
>
  </P
><P
>&#13;   <TT
CLASS="literal"
>case</TT
>
   表达式可以是任何求值为简单类型的表达式,即整型或浮点数以及字符串。不能用数组或对象,除非它们被解除引用成为简单类型。
  </P
><P
>&#13;   <TT
CLASS="literal"
>switch</TT
> 支持替代语法的流程控制。更多信息见<A
HREF="control-structures.alternative-syntax.html"
>流程控制的替代语法</A
>一节。
   <DIV
CLASS="informalexample"
><P
></P
><A
NAME="AEN4968"
></A
><TABLE
BORDER="0"
BGCOLOR="#E0E0E0"
CELLPADDING="5"
><TR
><TD
><code><font color="#000000">
<font color="#0000BB">&lt;?php<br /></font><font color="#007700">switch (</font><font color="#0000BB">$i</font><font color="#007700">):<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">0</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 0"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">1</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 1"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;case </font><font color="#0000BB">2</font><font color="#007700">:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i equals 2"</font><font color="#007700">;<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;break;<br />&nbsp;&nbsp;&nbsp;&nbsp;default:<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;echo </font><font color="#DD0000">"i is not equal to 0, 1 or 2"</font><font color="#007700">;<br />endswitch;<br /></font><font color="#0000BB">?&gt;</font>
</font>
</code></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"
>上一页</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>起始页</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="control-structures.declare.html"
ACCESSKEY="N"
>下一页</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="language.control-structures.html"
ACCESSKEY="U"
>上一级</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><TT
CLASS="literal"
>declare</TT
></TD
></TR
></TABLE
></DIV
></BODY
></HTML
>