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