xref: /PHP-7.0/tests/lang/foreachLoop.002.phpt (revision e5eb9530)
1--TEST--
2Foreach loop tests - modifying the array during the loop.
3--FILE--
4<?php
5
6echo "\nDirectly changing array values.\n";
7$a = array("original.1","original.2","original.3");
8foreach ($a as $k=>$v) {
9	$a[$k]="changed.$k";
10	var_dump($v);
11}
12var_dump($a);
13
14echo "\nModifying the foreach \$value.\n";
15$a = array("original.1","original.2","original.3");
16foreach ($a as $k=>$v) {
17	$v="changed.$k";
18}
19var_dump($a);
20
21
22echo "\nModifying the foreach &\$value.\n";
23$a = array("original.1","original.2","original.3");
24foreach ($a as $k=>&$v) {
25	$v="changed.$k";
26}
27var_dump($a);
28
29echo "\nPushing elements onto an unreferenced array.\n";
30$a = array("original.1","original.2","original.3");
31$counter=0;
32foreach ($a as $v) {
33	array_push($a, "new.$counter");
34
35	//avoid infinite loop if test is failing
36    if ($counter++>10) {
37    	echo "Loop detected\n";
38    	break;
39    }
40}
41var_dump($a);
42
43echo "\nPushing elements onto an unreferenced array, using &\$value.\n";
44$a = array("original.1","original.2","original.3");
45$counter=0;
46foreach ($a as &$v) {
47	array_push($a, "new.$counter");
48
49	//avoid infinite loop if test is failing
50    if ($counter++>10) {
51    	echo "Loop detected\n";
52    	break;
53    }
54}
55var_dump($a);
56
57echo "\nPopping elements off an unreferenced array.\n";
58$a = array("original.1","original.2","original.3");
59foreach ($a as $v) {
60	array_pop($a);
61	var_dump($v);
62}
63var_dump($a);
64
65echo "\nPopping elements off an unreferenced array, using &\$value.\n";
66$a = array("original.1","original.2","original.3");
67foreach ($a as &$v) {
68	array_pop($a);
69	var_dump($v);
70}
71var_dump($a);
72
73?>
74--EXPECT--
75
76Directly changing array values.
77string(10) "original.1"
78string(10) "original.2"
79string(10) "original.3"
80array(3) {
81  [0]=>
82  string(9) "changed.0"
83  [1]=>
84  string(9) "changed.1"
85  [2]=>
86  string(9) "changed.2"
87}
88
89Modifying the foreach $value.
90array(3) {
91  [0]=>
92  string(10) "original.1"
93  [1]=>
94  string(10) "original.2"
95  [2]=>
96  string(10) "original.3"
97}
98
99Modifying the foreach &$value.
100array(3) {
101  [0]=>
102  string(9) "changed.0"
103  [1]=>
104  string(9) "changed.1"
105  [2]=>
106  &string(9) "changed.2"
107}
108
109Pushing elements onto an unreferenced array.
110array(6) {
111  [0]=>
112  string(10) "original.1"
113  [1]=>
114  string(10) "original.2"
115  [2]=>
116  string(10) "original.3"
117  [3]=>
118  string(5) "new.0"
119  [4]=>
120  string(5) "new.1"
121  [5]=>
122  string(5) "new.2"
123}
124
125Pushing elements onto an unreferenced array, using &$value.
126Loop detected
127array(15) {
128  [0]=>
129  string(10) "original.1"
130  [1]=>
131  string(10) "original.2"
132  [2]=>
133  string(10) "original.3"
134  [3]=>
135  string(5) "new.0"
136  [4]=>
137  string(5) "new.1"
138  [5]=>
139  string(5) "new.2"
140  [6]=>
141  string(5) "new.3"
142  [7]=>
143  string(5) "new.4"
144  [8]=>
145  string(5) "new.5"
146  [9]=>
147  string(5) "new.6"
148  [10]=>
149  string(5) "new.7"
150  [11]=>
151  &string(5) "new.8"
152  [12]=>
153  string(5) "new.9"
154  [13]=>
155  string(6) "new.10"
156  [14]=>
157  string(6) "new.11"
158}
159
160Popping elements off an unreferenced array.
161string(10) "original.1"
162string(10) "original.2"
163string(10) "original.3"
164array(0) {
165}
166
167Popping elements off an unreferenced array, using &$value.
168string(10) "original.1"
169string(10) "original.2"
170array(1) {
171  [0]=>
172  string(10) "original.1"
173}
174