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
...
...
@@ -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 <type>string</type>s</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
]]>
...
...
@@ -195,6 +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>
212
+
</para>
213
+
<para>
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>
198
243
</para>
199
244
<para>
200
245
The alternative syntax for control structures is supported with
...
...
@@ -233,18 +278,28 @@ switch($beer)
233
278
{
234
279
case 'tuborg';
235
280
case 'carlsberg';
281
+
case 'stella';
236
282
case 'heineken';
237
283
echo 'Good choice';
238
-
break;
284
+
break;
239
285
default;
240
286
echo 'Please make a new selection...';
241
-
break;
287
+
break;
242
288
}
243
289
?>
244
290
]]>
245
291
</programlisting>
246
292
</informalexample>
247
293
</para>
294
+

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

250
305
<!-- Keep this comment at the end of the file
251
306