xref: /PHP-8.1/tests/lang/foreachLoop.013.phpt (revision f8d79582)
1--TEST--
2Directly modifying an unreferenced array when foreach'ing over it while using &$value syntax.
3--FILE--
4<?php
5
6define('MAX_LOOPS',5);
7
8function withRefValue($elements, $transform) {
9    echo "\n---( Array with $elements element(s): )---\n";
10    //Build array:
11    for ($i=0; $i<$elements; $i++) {
12        $a[] = "v.$i";
13    }
14    $counter=0;
15
16    echo "--> State of array before loop:\n";
17    var_dump($a);
18
19    echo "--> Do loop:\n";
20    foreach ($a as $k=>&$v) {
21        echo "     iteration $counter:  \$k=$k; \$v=$v\n";
22        eval($transform);
23        $counter++;
24        if ($counter>MAX_LOOPS) {
25            echo "  ** Stuck in a loop! **\n";
26            break;
27        }
28    }
29
30    echo "--> State of array after loop:\n";
31    var_dump($a);
32}
33
34
35echo "\nPopping elements off end of an unreferenced array, using &\$value.";
36$transform = 'array_pop($a);';
37withRefValue(1, $transform);
38withRefValue(2, $transform);
39withRefValue(3, $transform);
40withRefValue(4, $transform);
41
42echo "\n\n\nShift elements off start of an unreferenced array, using &\$value.";
43$transform = 'array_shift($a);';
44withRefValue(1, $transform);
45withRefValue(2, $transform);
46withRefValue(3, $transform);
47withRefValue(4, $transform);
48
49echo "\n\n\nRemove current element of an unreferenced array, using &\$value.";
50$transform = 'unset($a[$k]);';
51withRefValue(1, $transform);
52withRefValue(2, $transform);
53withRefValue(3, $transform);
54withRefValue(4, $transform);
55
56echo "\n\n\nAdding elements to the end of an unreferenced array, using &\$value.";
57$transform = 'array_push($a, "new.$counter");';
58withRefValue(1, $transform);
59withRefValue(2, $transform);
60withRefValue(3, $transform);
61withRefValue(4, $transform);
62
63echo "\n\n\nAdding elements to the start of an unreferenced array, using &\$value.";
64$transform = 'array_unshift($a, "new.$counter");';
65withRefValue(1, $transform);
66withRefValue(2, $transform);
67withRefValue(3, $transform);
68withRefValue(4, $transform);
69
70?>
71--EXPECT--
72Popping elements off end of an unreferenced array, using &$value.
73---( Array with 1 element(s): )---
74--> State of array before loop:
75array(1) {
76  [0]=>
77  string(3) "v.0"
78}
79--> Do loop:
80     iteration 0:  $k=0; $v=v.0
81--> State of array after loop:
82array(0) {
83}
84
85---( Array with 2 element(s): )---
86--> State of array before loop:
87array(2) {
88  [0]=>
89  string(3) "v.0"
90  [1]=>
91  string(3) "v.1"
92}
93--> Do loop:
94     iteration 0:  $k=0; $v=v.0
95--> State of array after loop:
96array(1) {
97  [0]=>
98  &string(3) "v.0"
99}
100
101---( Array with 3 element(s): )---
102--> State of array before loop:
103array(3) {
104  [0]=>
105  string(3) "v.0"
106  [1]=>
107  string(3) "v.1"
108  [2]=>
109  string(3) "v.2"
110}
111--> Do loop:
112     iteration 0:  $k=0; $v=v.0
113     iteration 1:  $k=1; $v=v.1
114--> State of array after loop:
115array(1) {
116  [0]=>
117  string(3) "v.0"
118}
119
120---( Array with 4 element(s): )---
121--> State of array before loop:
122array(4) {
123  [0]=>
124  string(3) "v.0"
125  [1]=>
126  string(3) "v.1"
127  [2]=>
128  string(3) "v.2"
129  [3]=>
130  string(3) "v.3"
131}
132--> Do loop:
133     iteration 0:  $k=0; $v=v.0
134     iteration 1:  $k=1; $v=v.1
135--> State of array after loop:
136array(2) {
137  [0]=>
138  string(3) "v.0"
139  [1]=>
140  &string(3) "v.1"
141}
142
143
144
145Shift elements off start of an unreferenced array, using &$value.
146---( Array with 1 element(s): )---
147--> State of array before loop:
148array(1) {
149  [0]=>
150  string(3) "v.0"
151}
152--> Do loop:
153     iteration 0:  $k=0; $v=v.0
154--> State of array after loop:
155array(0) {
156}
157
158---( Array with 2 element(s): )---
159--> State of array before loop:
160array(2) {
161  [0]=>
162  string(3) "v.0"
163  [1]=>
164  string(3) "v.1"
165}
166--> Do loop:
167     iteration 0:  $k=0; $v=v.0
168     iteration 1:  $k=0; $v=v.1
169--> State of array after loop:
170array(0) {
171}
172
173---( Array with 3 element(s): )---
174--> State of array before loop:
175array(3) {
176  [0]=>
177  string(3) "v.0"
178  [1]=>
179  string(3) "v.1"
180  [2]=>
181  string(3) "v.2"
182}
183--> Do loop:
184     iteration 0:  $k=0; $v=v.0
185     iteration 1:  $k=0; $v=v.1
186     iteration 2:  $k=0; $v=v.2
187--> State of array after loop:
188array(0) {
189}
190
191---( Array with 4 element(s): )---
192--> State of array before loop:
193array(4) {
194  [0]=>
195  string(3) "v.0"
196  [1]=>
197  string(3) "v.1"
198  [2]=>
199  string(3) "v.2"
200  [3]=>
201  string(3) "v.3"
202}
203--> Do loop:
204     iteration 0:  $k=0; $v=v.0
205     iteration 1:  $k=0; $v=v.1
206     iteration 2:  $k=0; $v=v.2
207     iteration 3:  $k=0; $v=v.3
208--> State of array after loop:
209array(0) {
210}
211
212
213
214Remove current element of an unreferenced array, using &$value.
215---( Array with 1 element(s): )---
216--> State of array before loop:
217array(1) {
218  [0]=>
219  string(3) "v.0"
220}
221--> Do loop:
222     iteration 0:  $k=0; $v=v.0
223--> State of array after loop:
224array(0) {
225}
226
227---( Array with 2 element(s): )---
228--> State of array before loop:
229array(2) {
230  [0]=>
231  string(3) "v.0"
232  [1]=>
233  string(3) "v.1"
234}
235--> Do loop:
236     iteration 0:  $k=0; $v=v.0
237     iteration 1:  $k=1; $v=v.1
238--> State of array after loop:
239array(0) {
240}
241
242---( Array with 3 element(s): )---
243--> State of array before loop:
244array(3) {
245  [0]=>
246  string(3) "v.0"
247  [1]=>
248  string(3) "v.1"
249  [2]=>
250  string(3) "v.2"
251}
252--> Do loop:
253     iteration 0:  $k=0; $v=v.0
254     iteration 1:  $k=1; $v=v.1
255     iteration 2:  $k=2; $v=v.2
256--> State of array after loop:
257array(0) {
258}
259
260---( Array with 4 element(s): )---
261--> State of array before loop:
262array(4) {
263  [0]=>
264  string(3) "v.0"
265  [1]=>
266  string(3) "v.1"
267  [2]=>
268  string(3) "v.2"
269  [3]=>
270  string(3) "v.3"
271}
272--> Do loop:
273     iteration 0:  $k=0; $v=v.0
274     iteration 1:  $k=1; $v=v.1
275     iteration 2:  $k=2; $v=v.2
276     iteration 3:  $k=3; $v=v.3
277--> State of array after loop:
278array(0) {
279}
280
281
282
283Adding elements to the end of an unreferenced array, using &$value.
284---( Array with 1 element(s): )---
285--> State of array before loop:
286array(1) {
287  [0]=>
288  string(3) "v.0"
289}
290--> Do loop:
291     iteration 0:  $k=0; $v=v.0
292     iteration 1:  $k=1; $v=new.0
293     iteration 2:  $k=2; $v=new.1
294     iteration 3:  $k=3; $v=new.2
295     iteration 4:  $k=4; $v=new.3
296     iteration 5:  $k=5; $v=new.4
297  ** Stuck in a loop! **
298--> State of array after loop:
299array(7) {
300  [0]=>
301  string(3) "v.0"
302  [1]=>
303  string(5) "new.0"
304  [2]=>
305  string(5) "new.1"
306  [3]=>
307  string(5) "new.2"
308  [4]=>
309  string(5) "new.3"
310  [5]=>
311  &string(5) "new.4"
312  [6]=>
313  string(5) "new.5"
314}
315
316---( Array with 2 element(s): )---
317--> State of array before loop:
318array(2) {
319  [0]=>
320  string(3) "v.0"
321  [1]=>
322  string(3) "v.1"
323}
324--> Do loop:
325     iteration 0:  $k=0; $v=v.0
326     iteration 1:  $k=1; $v=v.1
327     iteration 2:  $k=2; $v=new.0
328     iteration 3:  $k=3; $v=new.1
329     iteration 4:  $k=4; $v=new.2
330     iteration 5:  $k=5; $v=new.3
331  ** Stuck in a loop! **
332--> State of array after loop:
333array(8) {
334  [0]=>
335  string(3) "v.0"
336  [1]=>
337  string(3) "v.1"
338  [2]=>
339  string(5) "new.0"
340  [3]=>
341  string(5) "new.1"
342  [4]=>
343  string(5) "new.2"
344  [5]=>
345  &string(5) "new.3"
346  [6]=>
347  string(5) "new.4"
348  [7]=>
349  string(5) "new.5"
350}
351
352---( Array with 3 element(s): )---
353--> State of array before loop:
354array(3) {
355  [0]=>
356  string(3) "v.0"
357  [1]=>
358  string(3) "v.1"
359  [2]=>
360  string(3) "v.2"
361}
362--> Do loop:
363     iteration 0:  $k=0; $v=v.0
364     iteration 1:  $k=1; $v=v.1
365     iteration 2:  $k=2; $v=v.2
366     iteration 3:  $k=3; $v=new.0
367     iteration 4:  $k=4; $v=new.1
368     iteration 5:  $k=5; $v=new.2
369  ** Stuck in a loop! **
370--> State of array after loop:
371array(9) {
372  [0]=>
373  string(3) "v.0"
374  [1]=>
375  string(3) "v.1"
376  [2]=>
377  string(3) "v.2"
378  [3]=>
379  string(5) "new.0"
380  [4]=>
381  string(5) "new.1"
382  [5]=>
383  &string(5) "new.2"
384  [6]=>
385  string(5) "new.3"
386  [7]=>
387  string(5) "new.4"
388  [8]=>
389  string(5) "new.5"
390}
391
392---( Array with 4 element(s): )---
393--> State of array before loop:
394array(4) {
395  [0]=>
396  string(3) "v.0"
397  [1]=>
398  string(3) "v.1"
399  [2]=>
400  string(3) "v.2"
401  [3]=>
402  string(3) "v.3"
403}
404--> Do loop:
405     iteration 0:  $k=0; $v=v.0
406     iteration 1:  $k=1; $v=v.1
407     iteration 2:  $k=2; $v=v.2
408     iteration 3:  $k=3; $v=v.3
409     iteration 4:  $k=4; $v=new.0
410     iteration 5:  $k=5; $v=new.1
411  ** Stuck in a loop! **
412--> State of array after loop:
413array(10) {
414  [0]=>
415  string(3) "v.0"
416  [1]=>
417  string(3) "v.1"
418  [2]=>
419  string(3) "v.2"
420  [3]=>
421  string(3) "v.3"
422  [4]=>
423  string(5) "new.0"
424  [5]=>
425  &string(5) "new.1"
426  [6]=>
427  string(5) "new.2"
428  [7]=>
429  string(5) "new.3"
430  [8]=>
431  string(5) "new.4"
432  [9]=>
433  string(5) "new.5"
434}
435
436
437
438Adding elements to the start of an unreferenced array, using &$value.
439---( Array with 1 element(s): )---
440--> State of array before loop:
441array(1) {
442  [0]=>
443  string(3) "v.0"
444}
445--> Do loop:
446     iteration 0:  $k=0; $v=v.0
447--> State of array after loop:
448array(2) {
449  [0]=>
450  string(5) "new.0"
451  [1]=>
452  &string(3) "v.0"
453}
454
455---( Array with 2 element(s): )---
456--> State of array before loop:
457array(2) {
458  [0]=>
459  string(3) "v.0"
460  [1]=>
461  string(3) "v.1"
462}
463--> Do loop:
464     iteration 0:  $k=0; $v=v.0
465     iteration 1:  $k=2; $v=v.1
466--> State of array after loop:
467array(4) {
468  [0]=>
469  string(5) "new.1"
470  [1]=>
471  string(5) "new.0"
472  [2]=>
473  string(3) "v.0"
474  [3]=>
475  &string(3) "v.1"
476}
477
478---( Array with 3 element(s): )---
479--> State of array before loop:
480array(3) {
481  [0]=>
482  string(3) "v.0"
483  [1]=>
484  string(3) "v.1"
485  [2]=>
486  string(3) "v.2"
487}
488--> Do loop:
489     iteration 0:  $k=0; $v=v.0
490     iteration 1:  $k=2; $v=v.1
491     iteration 2:  $k=4; $v=v.2
492--> State of array after loop:
493array(6) {
494  [0]=>
495  string(5) "new.2"
496  [1]=>
497  string(5) "new.1"
498  [2]=>
499  string(5) "new.0"
500  [3]=>
501  string(3) "v.0"
502  [4]=>
503  string(3) "v.1"
504  [5]=>
505  &string(3) "v.2"
506}
507
508---( Array with 4 element(s): )---
509--> State of array before loop:
510array(4) {
511  [0]=>
512  string(3) "v.0"
513  [1]=>
514  string(3) "v.1"
515  [2]=>
516  string(3) "v.2"
517  [3]=>
518  string(3) "v.3"
519}
520--> Do loop:
521     iteration 0:  $k=0; $v=v.0
522     iteration 1:  $k=2; $v=v.1
523     iteration 2:  $k=4; $v=v.2
524     iteration 3:  $k=6; $v=v.3
525--> State of array after loop:
526array(8) {
527  [0]=>
528  string(5) "new.3"
529  [1]=>
530  string(5) "new.2"
531  [2]=>
532  string(5) "new.1"
533  [3]=>
534  string(5) "new.0"
535  [4]=>
536  string(3) "v.0"
537  [5]=>
538  string(3) "v.1"
539  [6]=>
540  string(3) "v.2"
541  [7]=>
542  &string(3) "v.3"
543}
544