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