xref: /PHP-5.4/tests/lang/foreachLoop.014.phpt (revision 176012df)
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=0; $v=v.0
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--> State of array after loop:
116array(1) {
117  [0]=>
118  string(3) "v.0"
119}
120
121---( Array with 4 element(s): )---
122--> State of referenced array before loop:
123array(4) {
124  [0]=>
125  string(3) "v.0"
126  [1]=>
127  string(3) "v.1"
128  [2]=>
129  string(3) "v.2"
130  [3]=>
131  string(3) "v.3"
132}
133--> Do loop:
134     iteration 0:  $k=0; $v=v.0
135     iteration 1:  $k=1; $v=v.1
136     iteration 2:  $k=0; $v=v.0
137     iteration 3:  $k=0; $v=v.0
138--> State of array after loop:
139array(0) {
140}
141
142
143
144Shift elements off start of a referenced array
145---( Array with 1 element(s): )---
146--> State of referenced array before loop:
147array(1) {
148  [0]=>
149  string(3) "v.0"
150}
151--> Do loop:
152     iteration 0:  $k=0; $v=v.0
153--> State of array after loop:
154array(0) {
155}
156
157---( Array with 2 element(s): )---
158--> State of referenced array before loop:
159array(2) {
160  [0]=>
161  string(3) "v.0"
162  [1]=>
163  string(3) "v.1"
164}
165--> Do loop:
166     iteration 0:  $k=0; $v=v.0
167     iteration 1:  $k=0; $v=v.1
168--> State of array after loop:
169array(0) {
170}
171
172---( Array with 3 element(s): )---
173--> State of referenced array before loop:
174array(3) {
175  [0]=>
176  string(3) "v.0"
177  [1]=>
178  string(3) "v.1"
179  [2]=>
180  string(3) "v.2"
181}
182--> Do loop:
183     iteration 0:  $k=0; $v=v.0
184     iteration 1:  $k=0; $v=v.1
185     iteration 2:  $k=0; $v=v.2
186--> State of array after loop:
187array(0) {
188}
189
190---( Array with 4 element(s): )---
191--> State of referenced array before loop:
192array(4) {
193  [0]=>
194  string(3) "v.0"
195  [1]=>
196  string(3) "v.1"
197  [2]=>
198  string(3) "v.2"
199  [3]=>
200  string(3) "v.3"
201}
202--> Do loop:
203     iteration 0:  $k=0; $v=v.0
204     iteration 1:  $k=0; $v=v.1
205     iteration 2:  $k=0; $v=v.2
206     iteration 3:  $k=0; $v=v.3
207--> State of array after loop:
208array(0) {
209}
210
211
212
213Remove current element of a referenced array
214---( Array with 1 element(s): )---
215--> State of referenced array before loop:
216array(1) {
217  [0]=>
218  string(3) "v.0"
219}
220--> Do loop:
221     iteration 0:  $k=0; $v=v.0
222--> State of array after loop:
223array(0) {
224}
225
226---( Array with 2 element(s): )---
227--> State of referenced array before loop:
228array(2) {
229  [0]=>
230  string(3) "v.0"
231  [1]=>
232  string(3) "v.1"
233}
234--> Do loop:
235     iteration 0:  $k=0; $v=v.0
236     iteration 1:  $k=1; $v=v.1
237--> State of array after loop:
238array(0) {
239}
240
241---( Array with 3 element(s): )---
242--> State of referenced array before loop:
243array(3) {
244  [0]=>
245  string(3) "v.0"
246  [1]=>
247  string(3) "v.1"
248  [2]=>
249  string(3) "v.2"
250}
251--> Do loop:
252     iteration 0:  $k=0; $v=v.0
253     iteration 1:  $k=1; $v=v.1
254     iteration 2:  $k=2; $v=v.2
255--> State of array after loop:
256array(0) {
257}
258
259---( Array with 4 element(s): )---
260--> State of referenced array before loop:
261array(4) {
262  [0]=>
263  string(3) "v.0"
264  [1]=>
265  string(3) "v.1"
266  [2]=>
267  string(3) "v.2"
268  [3]=>
269  string(3) "v.3"
270}
271--> Do loop:
272     iteration 0:  $k=0; $v=v.0
273     iteration 1:  $k=1; $v=v.1
274     iteration 2:  $k=2; $v=v.2
275     iteration 3:  $k=3; $v=v.3
276--> State of array after loop:
277array(0) {
278}
279
280
281
282Adding elements to the end of a referenced array
283---( Array with 1 element(s): )---
284--> State of referenced array before loop:
285array(1) {
286  [0]=>
287  string(3) "v.0"
288}
289--> Do loop:
290     iteration 0:  $k=0; $v=v.0
291--> State of array after loop:
292array(2) {
293  [0]=>
294  string(3) "v.0"
295  [1]=>
296  string(5) "new.0"
297}
298
299---( Array with 2 element(s): )---
300--> State of referenced array before loop:
301array(2) {
302  [0]=>
303  string(3) "v.0"
304  [1]=>
305  string(3) "v.1"
306}
307--> Do loop:
308     iteration 0:  $k=0; $v=v.0
309     iteration 1:  $k=1; $v=v.1
310     iteration 2:  $k=2; $v=new.0
311     iteration 3:  $k=3; $v=new.1
312     iteration 4:  $k=4; $v=new.2
313     iteration 5:  $k=5; $v=new.3
314  ** Stuck in a loop! **
315--> State of array after loop:
316array(8) {
317  [0]=>
318  string(3) "v.0"
319  [1]=>
320  string(3) "v.1"
321  [2]=>
322  string(5) "new.0"
323  [3]=>
324  string(5) "new.1"
325  [4]=>
326  string(5) "new.2"
327  [5]=>
328  string(5) "new.3"
329  [6]=>
330  string(5) "new.4"
331  [7]=>
332  string(5) "new.5"
333}
334
335---( Array with 3 element(s): )---
336--> State of referenced array before loop:
337array(3) {
338  [0]=>
339  string(3) "v.0"
340  [1]=>
341  string(3) "v.1"
342  [2]=>
343  string(3) "v.2"
344}
345--> Do loop:
346     iteration 0:  $k=0; $v=v.0
347     iteration 1:  $k=1; $v=v.1
348     iteration 2:  $k=2; $v=v.2
349     iteration 3:  $k=3; $v=new.0
350     iteration 4:  $k=4; $v=new.1
351     iteration 5:  $k=5; $v=new.2
352  ** Stuck in a loop! **
353--> State of array after loop:
354array(9) {
355  [0]=>
356  string(3) "v.0"
357  [1]=>
358  string(3) "v.1"
359  [2]=>
360  string(3) "v.2"
361  [3]=>
362  string(5) "new.0"
363  [4]=>
364  string(5) "new.1"
365  [5]=>
366  string(5) "new.2"
367  [6]=>
368  string(5) "new.3"
369  [7]=>
370  string(5) "new.4"
371  [8]=>
372  string(5) "new.5"
373}
374
375---( Array with 4 element(s): )---
376--> State of referenced array before loop:
377array(4) {
378  [0]=>
379  string(3) "v.0"
380  [1]=>
381  string(3) "v.1"
382  [2]=>
383  string(3) "v.2"
384  [3]=>
385  string(3) "v.3"
386}
387--> Do loop:
388     iteration 0:  $k=0; $v=v.0
389     iteration 1:  $k=1; $v=v.1
390     iteration 2:  $k=2; $v=v.2
391     iteration 3:  $k=3; $v=v.3
392     iteration 4:  $k=4; $v=new.0
393     iteration 5:  $k=5; $v=new.1
394  ** Stuck in a loop! **
395--> State of array after loop:
396array(10) {
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  [4]=>
406  string(5) "new.0"
407  [5]=>
408  string(5) "new.1"
409  [6]=>
410  string(5) "new.2"
411  [7]=>
412  string(5) "new.3"
413  [8]=>
414  string(5) "new.4"
415  [9]=>
416  string(5) "new.5"
417}
418
419
420
421Adding elements to the start of a referenced array
422---( Array with 1 element(s): )---
423--> State of referenced array before loop:
424array(1) {
425  [0]=>
426  string(3) "v.0"
427}
428--> Do loop:
429     iteration 0:  $k=0; $v=v.0
430--> State of array after loop:
431array(2) {
432  [0]=>
433  string(5) "new.0"
434  [1]=>
435  string(3) "v.0"
436}
437
438---( Array with 2 element(s): )---
439--> State of referenced array before loop:
440array(2) {
441  [0]=>
442  string(3) "v.0"
443  [1]=>
444  string(3) "v.1"
445}
446--> Do loop:
447     iteration 0:  $k=0; $v=v.0
448     iteration 1:  $k=0; $v=new.0
449     iteration 2:  $k=0; $v=new.1
450     iteration 3:  $k=0; $v=new.2
451     iteration 4:  $k=0; $v=new.3
452     iteration 5:  $k=0; $v=new.4
453  ** Stuck in a loop! **
454--> State of array after loop:
455array(8) {
456  [0]=>
457  string(5) "new.5"
458  [1]=>
459  string(5) "new.4"
460  [2]=>
461  string(5) "new.3"
462  [3]=>
463  string(5) "new.2"
464  [4]=>
465  string(5) "new.1"
466  [5]=>
467  string(5) "new.0"
468  [6]=>
469  string(3) "v.0"
470  [7]=>
471  string(3) "v.1"
472}
473
474---( Array with 3 element(s): )---
475--> State of referenced array before loop:
476array(3) {
477  [0]=>
478  string(3) "v.0"
479  [1]=>
480  string(3) "v.1"
481  [2]=>
482  string(3) "v.2"
483}
484--> Do loop:
485     iteration 0:  $k=0; $v=v.0
486     iteration 1:  $k=0; $v=new.0
487     iteration 2:  $k=0; $v=new.1
488     iteration 3:  $k=0; $v=new.2
489     iteration 4:  $k=0; $v=new.3
490     iteration 5:  $k=0; $v=new.4
491  ** Stuck in a loop! **
492--> State of array after loop:
493array(9) {
494  [0]=>
495  string(5) "new.5"
496  [1]=>
497  string(5) "new.4"
498  [2]=>
499  string(5) "new.3"
500  [3]=>
501  string(5) "new.2"
502  [4]=>
503  string(5) "new.1"
504  [5]=>
505  string(5) "new.0"
506  [6]=>
507  string(3) "v.0"
508  [7]=>
509  string(3) "v.1"
510  [8]=>
511  string(3) "v.2"
512}
513
514---( Array with 4 element(s): )---
515--> State of referenced array before loop:
516array(4) {
517  [0]=>
518  string(3) "v.0"
519  [1]=>
520  string(3) "v.1"
521  [2]=>
522  string(3) "v.2"
523  [3]=>
524  string(3) "v.3"
525}
526--> Do loop:
527     iteration 0:  $k=0; $v=v.0
528     iteration 1:  $k=0; $v=new.0
529     iteration 2:  $k=0; $v=new.1
530     iteration 3:  $k=0; $v=new.2
531     iteration 4:  $k=0; $v=new.3
532     iteration 5:  $k=0; $v=new.4
533  ** Stuck in a loop! **
534--> State of array after loop:
535array(10) {
536  [0]=>
537  string(5) "new.5"
538  [1]=>
539  string(5) "new.4"
540  [2]=>
541  string(5) "new.3"
542  [3]=>
543  string(5) "new.2"
544  [4]=>
545  string(5) "new.1"
546  [5]=>
547  string(5) "new.0"
548  [6]=>
549  string(3) "v.0"
550  [7]=>
551  string(3) "v.1"
552  [8]=>
553  string(3) "v.2"
554  [9]=>
555  string(3) "v.3"
556}