language/control-structures/switch.xml
cdc9d28d334bbc08386fecf8aade66080004a9dd
...
...
@@ -2,7 +2,7 @@
2
2
<!-- $Revision$ -->
3
3

4
4
<sect1 xml:id="control-structures.switch" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
5
-
<title><literal>switch</literal></title>
5
+
<title>switch</title>
6
6
<?phpdoc print-version-for="switch"?>
7
7
<simpara>
8
8
The <literal>switch</literal> statement is similar to a series of
...
...
@@ -16,8 +16,8 @@
16
16
<simpara>
17
17
Note that unlike some other languages, the
18
18
<link linkend="control-structures.continue">continue</link> statement
19
-
applies to switch and acts similar to <literal>break</literal>. If you
20
-
have a switch inside a loop and wish to continue to the next iteration of
19
+
applies to <literal>switch</literal> and acts similar to <literal>break</literal>. If you
20
+
have a <literal>switch</literal> inside a loop and wish to continue to the next iteration of
21
21
the outer loop, use <literal>continue 2</literal>.
22
22
</simpara>
23
23
</note>
...
...
@@ -27,44 +27,18 @@
27
27
<link linkend="types.comparisions-loose">loose comparison</link>.
28
28
</para>
29
29
</note>
30
+

30
31
<para>
31
-
<table>
32
-
&reftitle.changelog;
33
-
<tgroup cols="2">
34
-
<thead>
35
-
<row>
36
-
<entry>&Version;</entry>
37
-
<entry>&Description;</entry>
38
-
</row>
39
-
</thead>
40
-
<tbody>
41
-
<row>
42
-
<entry>7.0.0</entry>
43
-
<entry>
44
-
Multiple default cases will raise a <constant>E_COMPILE_ERROR</constant> error.
45
-
</entry>
46
-
</row>
47
-
</tbody>
48
-
</tgroup>
49
-
</table>
50
-
</para>
51
-
<para>
52
-
The following two examples are two different ways to write the
53
-
same thing, one using a series of <literal>if</literal> and
54
-
<literal>elseif</literal> statements, and the other using the
55
-
<literal>switch</literal> statement:
32
+
In the following example, each code block is equivalent.
33
+
One uses a series of <literal>if</literal> and
34
+
<literal>elseif</literal> statements, and the other a
35
+
<literal>switch</literal> statement. In each case, the output is the same.
56
36
<example>
57
37
<title><literal>switch</literal> structure</title>
58
38
<programlisting role="php">
59
39
<![CDATA[
60
40
<?php
61
-
if ($i == 0) {
62
-
echo "i equals 0";
63
-
} elseif ($i == 1) {
64
-
echo "i equals 1";
65
-
} elseif ($i == 2) {
66
-
echo "i equals 2";
67
-
}
41
+
// This switch statement:
68
42

69
43
switch ($i) {
70
44
case 0:
...
...
@@ -77,25 +51,15 @@ switch ($i) {
77
51
echo "i equals 2";
78
52
break;
79
53
}
80
-
?>
81
-
]]>
82
-
</programlisting>
83
-
</example>
84
-
<example>
85
-
<title><literal>switch</literal> structure allows usage of strings</title>
86
-
<programlisting role="php">
87
-
<![CDATA[
88
-
<?php
89
-
switch ($i) {
90
-
case "apple":
91
-
echo "i is apple";
92
-
break;
93
-
case "bar":
94
-
echo "i is bar";
95
-
break;
96
-
case "cake":
97
-
echo "i is cake";
98
-
break;
54
+

55
+
// Is equivalent to:
56
+

57
+
if ($i == 0) {
58
+
echo "i equals 0";
59
+
} elseif ($i == 1) {
60
+
echo "i equals 1";
61
+
} elseif ($i == 2) {
62
+
echo "i equals 2";
99
63
}
100
64
?>
101
65
]]>
...
...
@@ -108,7 +72,7 @@ switch ($i) {
108
72
<literal>switch</literal> statement executes line by line
109
73
(actually, statement by statement). In the beginning, no code is
110
74
executed. Only when a <literal>case</literal> statement is found
111
-
with a value that matches the value of the
75
+
whose expression evaluates to a value that matches the value of the
112
76
<literal>switch</literal> expression does PHP begin to execute the
113
77
statements. PHP continues to execute the statements until the end
114
78
of the <literal>switch</literal> block, or the first time it sees
...
...
@@ -158,13 +122,13 @@ switch ($i) {
158
122
<![CDATA[
159
123
<?php
160
124
switch ($i) {
161
-
case 0:
162
-
case 1:
163
-
case 2:
164
-
echo "i is less than 3 but not negative";
165
-
break;
166
-
case 3:
167
-
echo "i is 3";
125
+
case 0:
126
+
case 1:
127
+
case 2:
128
+
echo "i is less than 3 but not negative";
129
+
break;
130
+
case 3:
131
+
echo "i is 3";
168
132
}
169
133
?>
170
134
]]>
...
...
@@ -195,12 +159,87 @@ switch ($i) {
195
159
]]>
196
160
</programlisting>
197
161
</informalexample>
162
+
<note>
163
+
<simpara>
164
+
Multiple default cases will raise a
165
+
<constant>E_COMPILE_ERROR</constant> error.
166
+
</simpara>
167
+
</note>
168
+
<note>
169
+
<simpara>
170
+
Technically the <literal>default</literal> case may be listed
171
+
in any order. It will only be used if no other case matches.
172
+
However, by convention it is best to place it at the end as the
173
+
last branch.
174
+
</simpara>
175
+
</note>
176
+
</para>
177
+
<para>
178
+
If no <literal>case</literal> branch matches, and there is no <literal>default</literal>
179
+
branch, then no code will be executed, just as if no <literal>if</literal> statement was true.
180
+
</para>
181
+
<para>
182
+
A case value may be given as an expression. However, that expression will be
183
+
evaluated on its own and then loosely compared with the switch value. That means
184
+
it cannot be used for complex evaluations of the switch value. For example:
185
+
<informalexample>
186
+
<programlisting role="php">
187
+
<![CDATA[
188
+
<?php
189
+
$target = 1;
190
+
$start = 3;
191
+

192
+
switch ($target) {
193
+
case $start - 1:
194
+
print "A";
195
+
break;
196
+
case $start - 2:
197
+
print "B";
198
+
break;
199
+
case $start - 3:
200
+
print "C";
201
+
break;
202
+
case $start - 4:
203
+
print "D";
204
+
break;
205
+
}
206
+

207
+
// Prints "B"
208
+
?>
209
+
]]>
210
+
</programlisting>
211
+
</informalexample>
198
212
</para>
199
213
<para>
200
-
The <literal>case</literal> expression may be any expression that
201
-
evaluates to a simple type, that is, integer or floating-point
202
-
numbers and strings. Arrays or objects cannot be used here unless
203
-
they are dereferenced to a simple type.
214
+
For more complex comparisons, the value &true; may be used as the switch value.
215
+
Or, alternatively, <literal>if</literal>-<literal>else</literal> blocks instead of <literal>switch</literal>.
216
+
<informalexample>
217
+
<programlisting role="php">
218
+
<![CDATA[
219
+
<?php
220
+
$offset = 1;
221
+
$start = 3;
222
+

223
+
switch (true) {
224
+
case $start - $offset === 1:
225
+
print "A";
226
+
break;
227
+
case $start - $offset === 2:
228
+
print "B";
229
+
break;
230
+
case $start - $offset === 3:
231
+
print "C";
232
+
break;
233
+
case $start - $offset === 4:
234
+
print "D";
235
+
break;
236
+
}
237
+

238
+
// Prints "B"
239
+
?>
240
+
]]>
241
+
</programlisting>
242
+
</informalexample>
204
243
</para>
205
244
<para>
206
245
The alternative syntax for control structures is supported with
...
...
@@ -239,18 +278,28 @@ switch($beer)
239
278
{
240
279
case 'tuborg';
241
280
case 'carlsberg';
281
+
case 'stella';
242
282
case 'heineken';
243
283
echo 'Good choice';
244
-
break;
284
+
break;
245
285
default;
246
286
echo 'Please make a new selection...';
247
-
break;
287
+
break;
248
288
}
249
289
?>
250
290
]]>
251
291
</programlisting>
252
292
</informalexample>
253
293
</para>
294
+

295
+
<sect2 role="seealso">
296
+
&reftitle.seealso;
297
+
<para>
298
+
<simplelist>
299
+
<member>&match;</member>
300
+
</simplelist>
301
+
</para>
302
+
</sect2>
254
303
</sect1>
255
304

256
305
<!-- Keep this comment at the end of the file
257
306