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