xref: /PHP-7.2/tests/lang/foreachLoop.014.phpt (revision 17ccbeec)
1--TEST--
2Directly modifying a REFERENCED array when foreach'ing over it.
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";
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";
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";
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";
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";
66$transform = 'array_unshift($a, "new.$counter");';
67withRefValue(1, $transform);
68withRefValue(2, $transform);
69withRefValue(3, $transform);
70withRefValue(4, $transform);
71
72?>
73--EXPECTF--
74Popping elements off end of a referenced array
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     iteration 1:  $k=1; $v=v.1
98--> State of array after loop:
99array(0) {
100}
101
102---( Array with 3 element(s): )---
103--> State of referenced array before loop:
104array(3) {
105  [0]=>
106  string(3) "v.0"
107  [1]=>
108  string(3) "v.1"
109  [2]=>
110  string(3) "v.2"
111}
112--> Do loop:
113     iteration 0:  $k=0; $v=v.0
114     iteration 1:  $k=1; $v=v.1
115     iteration 2:  $k=2; $v=v.2
116--> State of array after loop:
117array(0) {
118}
119
120---( Array with 4 element(s): )---
121--> State of referenced 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     iteration 2:  $k=2; $v=v.2
136     iteration 3:  $k=3; $v=v.3
137--> State of array after loop:
138array(0) {
139}
140
141
142
143Shift elements off start of a referenced array
144---( Array with 1 element(s): )---
145--> State of referenced array before loop:
146array(1) {
147  [0]=>
148  string(3) "v.0"
149}
150--> Do loop:
151     iteration 0:  $k=0; $v=v.0
152--> State of array after loop:
153array(0) {
154}
155
156---( Array with 2 element(s): )---
157--> State of referenced array before loop:
158array(2) {
159  [0]=>
160  string(3) "v.0"
161  [1]=>
162  string(3) "v.1"
163}
164--> Do loop:
165     iteration 0:  $k=0; $v=v.0
166     iteration 1:  $k=1; $v=v.1
167--> State of array after loop:
168array(0) {
169}
170
171---( Array with 3 element(s): )---
172--> State of referenced array before loop:
173array(3) {
174  [0]=>
175  string(3) "v.0"
176  [1]=>
177  string(3) "v.1"
178  [2]=>
179  string(3) "v.2"
180}
181--> Do loop:
182     iteration 0:  $k=0; $v=v.0
183     iteration 1:  $k=1; $v=v.1
184     iteration 2:  $k=2; $v=v.2
185--> State of array after loop:
186array(0) {
187}
188
189---( Array with 4 element(s): )---
190--> State of referenced array before loop:
191array(4) {
192  [0]=>
193  string(3) "v.0"
194  [1]=>
195  string(3) "v.1"
196  [2]=>
197  string(3) "v.2"
198  [3]=>
199  string(3) "v.3"
200}
201--> Do loop:
202     iteration 0:  $k=0; $v=v.0
203     iteration 1:  $k=1; $v=v.1
204     iteration 2:  $k=2; $v=v.2
205     iteration 3:  $k=3; $v=v.3
206--> State of array after loop:
207array(0) {
208}
209
210
211
212Remove current element of a referenced array
213---( Array with 1 element(s): )---
214--> State of referenced array before loop:
215array(1) {
216  [0]=>
217  string(3) "v.0"
218}
219--> Do loop:
220     iteration 0:  $k=0; $v=v.0
221--> State of array after loop:
222array(0) {
223}
224
225---( Array with 2 element(s): )---
226--> State of referenced array before loop:
227array(2) {
228  [0]=>
229  string(3) "v.0"
230  [1]=>
231  string(3) "v.1"
232}
233--> Do loop:
234     iteration 0:  $k=0; $v=v.0
235     iteration 1:  $k=1; $v=v.1
236--> State of array after loop:
237array(0) {
238}
239
240---( Array with 3 element(s): )---
241--> State of referenced array before loop:
242array(3) {
243  [0]=>
244  string(3) "v.0"
245  [1]=>
246  string(3) "v.1"
247  [2]=>
248  string(3) "v.2"
249}
250--> Do loop:
251     iteration 0:  $k=0; $v=v.0
252     iteration 1:  $k=1; $v=v.1
253     iteration 2:  $k=2; $v=v.2
254--> State of array after loop:
255array(0) {
256}
257
258---( Array with 4 element(s): )---
259--> State of referenced array before loop:
260array(4) {
261  [0]=>
262  string(3) "v.0"
263  [1]=>
264  string(3) "v.1"
265  [2]=>
266  string(3) "v.2"
267  [3]=>
268  string(3) "v.3"
269}
270--> Do loop:
271     iteration 0:  $k=0; $v=v.0
272     iteration 1:  $k=1; $v=v.1
273     iteration 2:  $k=2; $v=v.2
274     iteration 3:  $k=3; $v=v.3
275--> State of array after loop:
276array(0) {
277}
278
279
280
281Adding elements to the end of a referenced array
282---( Array with 1 element(s): )---
283--> State of referenced array before loop:
284array(1) {
285  [0]=>
286  string(3) "v.0"
287}
288--> Do loop:
289     iteration 0:  $k=0; $v=v.0
290--> State of array after loop:
291array(2) {
292  [0]=>
293  string(3) "v.0"
294  [1]=>
295  string(5) "new.0"
296}
297
298---( Array with 2 element(s): )---
299--> State of referenced array before loop:
300array(2) {
301  [0]=>
302  string(3) "v.0"
303  [1]=>
304  string(3) "v.1"
305}
306--> Do loop:
307     iteration 0:  $k=0; $v=v.0
308     iteration 1:  $k=1; $v=v.1
309--> State of array after loop:
310array(4) {
311  [0]=>
312  string(3) "v.0"
313  [1]=>
314  string(3) "v.1"
315  [2]=>
316  string(5) "new.0"
317  [3]=>
318  string(5) "new.1"
319}
320
321---( Array with 3 element(s): )---
322--> State of referenced array before loop:
323array(3) {
324  [0]=>
325  string(3) "v.0"
326  [1]=>
327  string(3) "v.1"
328  [2]=>
329  string(3) "v.2"
330}
331--> Do loop:
332     iteration 0:  $k=0; $v=v.0
333     iteration 1:  $k=1; $v=v.1
334     iteration 2:  $k=2; $v=v.2
335--> State of array after loop:
336array(6) {
337  [0]=>
338  string(3) "v.0"
339  [1]=>
340  string(3) "v.1"
341  [2]=>
342  string(3) "v.2"
343  [3]=>
344  string(5) "new.0"
345  [4]=>
346  string(5) "new.1"
347  [5]=>
348  string(5) "new.2"
349}
350
351---( Array with 4 element(s): )---
352--> State of referenced array before loop:
353array(4) {
354  [0]=>
355  string(3) "v.0"
356  [1]=>
357  string(3) "v.1"
358  [2]=>
359  string(3) "v.2"
360  [3]=>
361  string(3) "v.3"
362}
363--> Do loop:
364     iteration 0:  $k=0; $v=v.0
365     iteration 1:  $k=1; $v=v.1
366     iteration 2:  $k=2; $v=v.2
367     iteration 3:  $k=3; $v=v.3
368--> State of array after loop:
369array(8) {
370  [0]=>
371  string(3) "v.0"
372  [1]=>
373  string(3) "v.1"
374  [2]=>
375  string(3) "v.2"
376  [3]=>
377  string(3) "v.3"
378  [4]=>
379  string(5) "new.0"
380  [5]=>
381  string(5) "new.1"
382  [6]=>
383  string(5) "new.2"
384  [7]=>
385  string(5) "new.3"
386}
387
388
389
390Adding elements to the start of a referenced array
391---( Array with 1 element(s): )---
392--> State of referenced array before loop:
393array(1) {
394  [0]=>
395  string(3) "v.0"
396}
397--> Do loop:
398     iteration 0:  $k=0; $v=v.0
399--> State of array after loop:
400array(2) {
401  [0]=>
402  string(5) "new.0"
403  [1]=>
404  string(3) "v.0"
405}
406
407---( Array with 2 element(s): )---
408--> State of referenced array before loop:
409array(2) {
410  [0]=>
411  string(3) "v.0"
412  [1]=>
413  string(3) "v.1"
414}
415--> Do loop:
416     iteration 0:  $k=0; $v=v.0
417     iteration 1:  $k=1; $v=v.1
418--> State of array after loop:
419array(4) {
420  [0]=>
421  string(5) "new.1"
422  [1]=>
423  string(5) "new.0"
424  [2]=>
425  string(3) "v.0"
426  [3]=>
427  string(3) "v.1"
428}
429
430---( Array with 3 element(s): )---
431--> State of referenced array before loop:
432array(3) {
433  [0]=>
434  string(3) "v.0"
435  [1]=>
436  string(3) "v.1"
437  [2]=>
438  string(3) "v.2"
439}
440--> Do loop:
441     iteration 0:  $k=0; $v=v.0
442     iteration 1:  $k=1; $v=v.1
443     iteration 2:  $k=2; $v=v.2
444--> State of array after loop:
445array(6) {
446  [0]=>
447  string(5) "new.2"
448  [1]=>
449  string(5) "new.1"
450  [2]=>
451  string(5) "new.0"
452  [3]=>
453  string(3) "v.0"
454  [4]=>
455  string(3) "v.1"
456  [5]=>
457  string(3) "v.2"
458}
459
460---( Array with 4 element(s): )---
461--> State of referenced array before loop:
462array(4) {
463  [0]=>
464  string(3) "v.0"
465  [1]=>
466  string(3) "v.1"
467  [2]=>
468  string(3) "v.2"
469  [3]=>
470  string(3) "v.3"
471}
472--> Do loop:
473     iteration 0:  $k=0; $v=v.0
474     iteration 1:  $k=1; $v=v.1
475     iteration 2:  $k=2; $v=v.2
476     iteration 3:  $k=3; $v=v.3
477--> State of array after loop:
478array(8) {
479  [0]=>
480  string(5) "new.3"
481  [1]=>
482  string(5) "new.2"
483  [2]=>
484  string(5) "new.1"
485  [3]=>
486  string(5) "new.0"
487  [4]=>
488  string(3) "v.0"
489  [5]=>
490  string(3) "v.1"
491  [6]=>
492  string(3) "v.2"
493  [7]=>
494  string(3) "v.3"
495}
496