xref: /PHP-5.5/tests/lang/foreachLoop.013.phpt (revision 176012df)
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--EXPECTF--
72
73Popping elements off end of an unreferenced array, using &$value.
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=0; $v=v.0
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--> 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     iteration 2:  $k=0; $v=v.0
136     iteration 3:  $k=0; $v=v.0
137--> State of array after loop:
138array(0) {
139}
140
141
142
143Shift elements off start of an unreferenced array, using &$value.
144---( Array with 1 element(s): )---
145--> State of 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 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=0; $v=v.1
167--> State of array after loop:
168array(0) {
169}
170
171---( Array with 3 element(s): )---
172--> State of 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=0; $v=v.1
184     iteration 2:  $k=0; $v=v.2
185--> State of array after loop:
186array(0) {
187}
188
189---( Array with 4 element(s): )---
190--> State of 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=0; $v=v.1
204     iteration 2:  $k=0; $v=v.2
205     iteration 3:  $k=0; $v=v.3
206--> State of array after loop:
207array(0) {
208}
209
210
211
212Remove current element of an unreferenced array, using &$value.
213---( Array with 1 element(s): )---
214--> State of 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 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 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 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 an unreferenced array, using &$value.
282---( Array with 1 element(s): )---
283--> State of 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 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     iteration 2:  $k=2; $v=new.0
310     iteration 3:  $k=3; $v=new.1
311     iteration 4:  $k=4; $v=new.2
312     iteration 5:  $k=5; $v=new.3
313  ** Stuck in a loop! **
314--> State of array after loop:
315array(8) {
316  [0]=>
317  string(3) "v.0"
318  [1]=>
319  string(3) "v.1"
320  [2]=>
321  string(5) "new.0"
322  [3]=>
323  string(5) "new.1"
324  [4]=>
325  string(5) "new.2"
326  [5]=>
327  &string(5) "new.3"
328  [6]=>
329  string(5) "new.4"
330  [7]=>
331  string(5) "new.5"
332}
333
334---( Array with 3 element(s): )---
335--> State of array before loop:
336array(3) {
337  [0]=>
338  string(3) "v.0"
339  [1]=>
340  string(3) "v.1"
341  [2]=>
342  string(3) "v.2"
343}
344--> Do loop:
345     iteration 0:  $k=0; $v=v.0
346     iteration 1:  $k=1; $v=v.1
347     iteration 2:  $k=2; $v=v.2
348     iteration 3:  $k=3; $v=new.0
349     iteration 4:  $k=4; $v=new.1
350     iteration 5:  $k=5; $v=new.2
351  ** Stuck in a loop! **
352--> State of array after loop:
353array(9) {
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(5) "new.0"
362  [4]=>
363  string(5) "new.1"
364  [5]=>
365  &string(5) "new.2"
366  [6]=>
367  string(5) "new.3"
368  [7]=>
369  string(5) "new.4"
370  [8]=>
371  string(5) "new.5"
372}
373
374---( Array with 4 element(s): )---
375--> State of array before loop:
376array(4) {
377  [0]=>
378  string(3) "v.0"
379  [1]=>
380  string(3) "v.1"
381  [2]=>
382  string(3) "v.2"
383  [3]=>
384  string(3) "v.3"
385}
386--> Do loop:
387     iteration 0:  $k=0; $v=v.0
388     iteration 1:  $k=1; $v=v.1
389     iteration 2:  $k=2; $v=v.2
390     iteration 3:  $k=3; $v=v.3
391     iteration 4:  $k=4; $v=new.0
392     iteration 5:  $k=5; $v=new.1
393  ** Stuck in a loop! **
394--> State of array after loop:
395array(10) {
396  [0]=>
397  string(3) "v.0"
398  [1]=>
399  string(3) "v.1"
400  [2]=>
401  string(3) "v.2"
402  [3]=>
403  string(3) "v.3"
404  [4]=>
405  string(5) "new.0"
406  [5]=>
407  &string(5) "new.1"
408  [6]=>
409  string(5) "new.2"
410  [7]=>
411  string(5) "new.3"
412  [8]=>
413  string(5) "new.4"
414  [9]=>
415  string(5) "new.5"
416}
417
418
419
420Adding elements to the start of an unreferenced array, using &$value.
421---( Array with 1 element(s): )---
422--> State of array before loop:
423array(1) {
424  [0]=>
425  string(3) "v.0"
426}
427--> Do loop:
428     iteration 0:  $k=0; $v=v.0
429--> State of array after loop:
430array(2) {
431  [0]=>
432  string(5) "new.0"
433  [1]=>
434  &string(3) "v.0"
435}
436
437---( Array with 2 element(s): )---
438--> State of array before loop:
439array(2) {
440  [0]=>
441  string(3) "v.0"
442  [1]=>
443  string(3) "v.1"
444}
445--> Do loop:
446     iteration 0:  $k=0; $v=v.0
447     iteration 1:  $k=0; $v=new.0
448     iteration 2:  $k=0; $v=new.1
449     iteration 3:  $k=0; $v=new.2
450     iteration 4:  $k=0; $v=new.3
451     iteration 5:  $k=0; $v=new.4
452  ** Stuck in a loop! **
453--> State of array after loop:
454array(8) {
455  [0]=>
456  string(5) "new.5"
457  [1]=>
458  &string(5) "new.4"
459  [2]=>
460  string(5) "new.3"
461  [3]=>
462  string(5) "new.2"
463  [4]=>
464  string(5) "new.1"
465  [5]=>
466  string(5) "new.0"
467  [6]=>
468  string(3) "v.0"
469  [7]=>
470  string(3) "v.1"
471}
472
473---( Array with 3 element(s): )---
474--> State of array before loop:
475array(3) {
476  [0]=>
477  string(3) "v.0"
478  [1]=>
479  string(3) "v.1"
480  [2]=>
481  string(3) "v.2"
482}
483--> Do loop:
484     iteration 0:  $k=0; $v=v.0
485     iteration 1:  $k=0; $v=new.0
486     iteration 2:  $k=0; $v=new.1
487     iteration 3:  $k=0; $v=new.2
488     iteration 4:  $k=0; $v=new.3
489     iteration 5:  $k=0; $v=new.4
490  ** Stuck in a loop! **
491--> State of array after loop:
492array(9) {
493  [0]=>
494  string(5) "new.5"
495  [1]=>
496  &string(5) "new.4"
497  [2]=>
498  string(5) "new.3"
499  [3]=>
500  string(5) "new.2"
501  [4]=>
502  string(5) "new.1"
503  [5]=>
504  string(5) "new.0"
505  [6]=>
506  string(3) "v.0"
507  [7]=>
508  string(3) "v.1"
509  [8]=>
510  string(3) "v.2"
511}
512
513---( Array with 4 element(s): )---
514--> State of array before loop:
515array(4) {
516  [0]=>
517  string(3) "v.0"
518  [1]=>
519  string(3) "v.1"
520  [2]=>
521  string(3) "v.2"
522  [3]=>
523  string(3) "v.3"
524}
525--> Do loop:
526     iteration 0:  $k=0; $v=v.0
527     iteration 1:  $k=0; $v=new.0
528     iteration 2:  $k=0; $v=new.1
529     iteration 3:  $k=0; $v=new.2
530     iteration 4:  $k=0; $v=new.3
531     iteration 5:  $k=0; $v=new.4
532  ** Stuck in a loop! **
533--> State of array after loop:
534array(10) {
535  [0]=>
536  string(5) "new.5"
537  [1]=>
538  &string(5) "new.4"
539  [2]=>
540  string(5) "new.3"
541  [3]=>
542  string(5) "new.2"
543  [4]=>
544  string(5) "new.1"
545  [5]=>
546  string(5) "new.0"
547  [6]=>
548  string(3) "v.0"
549  [7]=>
550  string(3) "v.1"
551  [8]=>
552  string(3) "v.2"
553  [9]=>
554  string(3) "v.3"
555}
556