language/control-structures/match.xml
17502ebe0691a84e7d0738c13e8c1061dde98de7
...
...
@@ -29,6 +29,58 @@ $return_value = match (subject_expression) {
29
29
]]>
30
30
</programlisting>
31
31
32
+
<example>
33
+
<title>Basic <literal>match</literal> usage</title>
34
+
<programlisting role="php">
35
+
<![CDATA[
36
+
<?php
37
+
$food = 'cake';
38
+

39
+
$return_value = match ($food) {
40
+
'apple' => 'This food is an apple',
41
+
'bar' => 'This food is a bar',
42
+
'cake' => 'This food is a cake',
43
+
};
44
+

45
+
var_dump($return_value);
46
+
?>
47
+
]]>
48
+
</programlisting>
49
+
&example.outputs;
50
+
<screen>
51
+
<![CDATA[
52
+
string(19) "This food is a cake"
53
+
]]>
54
+
</screen>
55
+
</example>
56
+

57
+
<example>
58
+
<title>Example of using <literal>match</literal> with comparison operators</title>
59
+
<programlisting role="php">
60
+
<![CDATA[
61
+
<?php
62
+
$age = 18;
63
+

64
+
$output = match (true) {
65
+
$age < 2 => "Baby",
66
+
$age < 13 => "Child",
67
+
$age <= 19 => "Teenager",
68
+
$age >= 40 => "Old adult",
69
+
$age > 19 => "Young adult",
70
+
};
71
+

72
+
var_dump($output);
73
+
?>
74
+
]]>
75
+
</programlisting>
76
+
&example.outputs;
77
+
<screen>
78
+
<![CDATA[
79
+
string(8) "Teenager"
80
+
]]>
81
+
</screen>
82
+
</example>
83
+

32
84
<note>
33
85
<simpara>
34
86
The result of a <literal>match</literal> expression does not need to be used.
...
...
@@ -36,8 +88,9 @@ $return_value = match (subject_expression) {
36
88
</note>
37
89
<note>
38
90
<simpara>
39
-
A <literal>match</literal> expression <emphasis>must</emphasis> be
40
-
terminated by a semicolon <literal>;</literal>.
91
+
When a <literal>match</literal> expression is used as a standalone
92
+
expression it <emphasis>must</emphasis> be terminated
93
+
by a semicolon <literal>;</literal>.
41
94
</simpara>
42
95
</note>
43
96
</example>
...
...
@@ -60,7 +113,7 @@ $return_value = match (subject_expression) {
60
113
</listitem>
61
114
<listitem>
62
115
<simpara>
63
-
<literal>Match</literal> arms do not fall-through to later cases the way
116
+
<literal>match</literal> arms do not fall-through to later cases the way
64
117
<literal>switch</literal> statements do.
65
118
</simpara>
66
119
</listitem>
...
...
@@ -86,8 +139,8 @@ $return_value = match (subject_expression) {
86
139
<![CDATA[
87
140
<?php
88
141
$result = match ($x) {
89
-
foo() => ...,
90
-
$this->bar() => ..., // bar() isn't called if foo() === $x
142
+
foo() => 'value',
143
+
$this->bar() => 'value', // $this->bar() isn't called if foo() === $x
91
144
$this->baz => beep(), // beep() isn't called unless $x === $this->baz
92
145
// etc.
93
146
};
...
...
@@ -237,8 +290,8 @@ string(11) "young adult"
237
290
$text = 'Bienvenue chez nous';
238
291

239
292
$result = match (true) {
240
-
str_contains($text, 'Welcome') || str_contains($text, 'Hello') => 'en',
241
-
str_contains($text, 'Bienvenue') || str_contains($text, 'Bonjour') => 'fr',
293
+
str_contains($text, 'Welcome'), str_contains($text, 'Hello') => 'en',
294
+
str_contains($text, 'Bienvenue'), str_contains($text, 'Bonjour') => 'fr',
242
295
// ...
243
296
};
244
297

245
298