reference/array/functions/list.xml
2e60c5134e7a847c99f81eb3f7ecee1f5efeeace
2e60c5134e7a847c99f81eb3f7ecee1f5efeeace
...
...
@@ -14,8 +14,10 @@
14
14
</methodsynopsis>
15
15
<para>
16
16
Like <function>array</function>, this is not really a function,
17
-
but a language construct. <function>list</function> is used to
17
+
but a language construct. <function>list</function> is used to
18
18
assign a list of variables in one operation.
19
+
Strings can not be unpacked and <function>list</function> expressions
20
+
can not be completely empty.
19
21
</para>
20
22
<note>
21
23
<para>
...
...
@@ -23,24 +25,11 @@
23
25
the numerical indices start at 0.
24
26
</para>
25
27
</note>
26
-
<warning>
27
-
<para>
28
-
In PHP 5, <function>list</function> assigns the values starting with the
29
-
right-most parameter. In PHP 7, <function>list</function> starts with the
30
-
left-most parameter.
31
-
</para>
32
-
<para>
33
-
If you are using plain variables, you don't have to worry about this. But
34
-
if you are using arrays with indices you usually expect the order of the
35
-
indices in the array the same you wrote in the <function>list</function>
36
-
from left to right, which is not the case in PHP 5, as it's assigned in the
37
-
reverse order.
38
-
</para>
39
-
<para>
40
-
Generally speaking, it is advisable to avoid relying on a specific order
41
-
of operation, as this may change again in the future.
42
-
</para>
43
-
</warning>
28
+
<para>
29
+
As of PHP 7.1.0, <function>list</function> can also contain explicit keys, allowing for the
30
+
destructuring of arrays with non-integer or non-sequential keys. For more details on
31
+
array destructuring, see the <link linkend="language.types.array.syntax.destructuring">array destructuring section</link>.
32
+
</para>
44
33
</refsect1>
45
34
<refsect1 role="parameters">
46
35
&reftitle.parameters;
...
...
@@ -98,26 +87,6 @@
98
87
enables destructuring of arrays with non-integer or non-sequential keys.
99
88
</entry>
100
89
</row>
101
-
<row>
102
-
<entry>7.0.0</entry>
103
-
<entry>
104
-
The order that the assignment operations are performed in has
105
-
changed.
106
-
</entry>
107
-
</row>
108
-
<row>
109
-
<entry>7.0.0</entry>
110
-
<entry>
111
-
<function>list</function> expressions can no longer be completely
112
-
empty.
113
-
</entry>
114
-
</row>
115
-
<row>
116
-
<entry>7.0.0</entry>
117
-
<entry>
118
-
Strings can no longer be unpacked.
119
-
</entry>
120
-
</row>
121
90
</tbody>
122
91
</tgroup>
123
92
</informaltable>
...
...
@@ -157,26 +126,14 @@ var_dump($bar); // NULL
157
126
<para>
158
127
<example>
159
128
<title>An example use of <function>list</function></title>
160
-
<programlisting role="php">
129
+
<programlisting role="php" annotations="non-interactive">
161
130
<![CDATA[
162
-
<table>
163
-
<tr>
164
-
<th>Employee name</th>
165
-
<th>Salary</th>
166
-
</tr>
167
-
168
131
<?php
169
-
$result = $pdo->query("SELECT id, name, salary FROM employees");
170
-
while (list($id, $name, $salary) = $result->fetch(PDO::FETCH_NUM)) {
171
-
echo " <tr>\n" .
172
-
" <td><a href=\"info.php?id=$id\">$name</a></td>\n" .
173
-
" <td>$salary</td>\n" .
174
-
" </tr>\n";
132
+
$result = $pdo->query("SELECT id, name FROM employees");
133
+
while (list($id, $name) = $result->fetch(PDO::FETCH_NUM)) {
134
+
echo "id: $id, name: $name\n";
175
135
}
176
-
177
136
?>
178
-
179
-
</table>
180
137
]]>
181
138
</programlisting>
182
139
</example>
...
...
@@ -195,6 +152,7 @@ var_dump($a, $b, $c);
195
152
?>
196
153
]]>
197
154
</programlisting>
155
+
&example.outputs;
198
156
<screen>
199
157
<![CDATA[
200
158
int(1)
...
...
@@ -205,60 +163,12 @@ int(3)
205
163
</example>
206
164
</para>
207
165
<para>
208
-
<example>
209
-
<title>Using <function>list</function> with array indices</title>
210
-
<programlisting role="php">
211
-
<![CDATA[
212
-
<?php
213
-
214
-
$info = array('coffee', 'brown', 'caffeine');
215
-
216
-
list($a[0], $a[1], $a[2]) = $info;
217
-
218
-
var_dump($a);
219
-
220
-
?>
221
-
]]>
222
-
</programlisting>
223
-
<para>
224
-
Gives the following output (note the order of the elements compared in
225
-
which order they were written in the <function>list</function> syntax):
226
-
</para>
227
-
&example.outputs.7;
228
-
<screen>
229
-
<![CDATA[
230
-
array(3) {
231
-
[0]=>
232
-
string(6) "coffee"
233
-
[1]=>
234
-
string(5) "brown"
235
-
[2]=>
236
-
string(8) "caffeine"
237
-
}
238
-
]]>
239
-
</screen>
240
-
&example.outputs.5;
241
-
<screen>
242
-
<![CDATA[
243
-
array(3) {
244
-
[2]=>
245
-
string(8) "caffeine"
246
-
[1]=>
247
-
string(5) "brown"
248
-
[0]=>
249
-
string(6) "coffee"
250
-
}
251
-
]]>
252
-
</screen>
253
-
</example>
166
+
The order in which the indices of the array to be consumed by
167
+
<function>list</function> are defined is irrelevant.
254
168
</para>
255
169
<para>
256
170
<example>
257
171
<title><function>list</function> and order of index definitions</title>
258
-
<simpara>
259
-
The order in which the indices of the array to be consumed by
260
-
<function>list</function> are defined is irrelevant.
261
-
</simpara>
262
172
<programlisting role="php">
263
173
<![CDATA[
264
174
<?php
265
175