xref: /PHP-8.0/tests/lang/foreachLoop.002.phpt (revision f8d79582)
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--
75Directly changing array values.
76string(10) "original.1"
77string(10) "original.2"
78string(10) "original.3"
79array(3) {
80  [0]=>
81  string(9) "changed.0"
82  [1]=>
83  string(9) "changed.1"
84  [2]=>
85  string(9) "changed.2"
86}
87
88Modifying the foreach $value.
89array(3) {
90  [0]=>
91  string(10) "original.1"
92  [1]=>
93  string(10) "original.2"
94  [2]=>
95  string(10) "original.3"
96}
97
98Modifying the foreach &$value.
99array(3) {
100  [0]=>
101  string(9) "changed.0"
102  [1]=>
103  string(9) "changed.1"
104  [2]=>
105  &string(9) "changed.2"
106}
107
108Pushing elements onto an unreferenced array.
109array(6) {
110  [0]=>
111  string(10) "original.1"
112  [1]=>
113  string(10) "original.2"
114  [2]=>
115  string(10) "original.3"
116  [3]=>
117  string(5) "new.0"
118  [4]=>
119  string(5) "new.1"
120  [5]=>
121  string(5) "new.2"
122}
123
124Pushing elements onto an unreferenced array, using &$value.
125Loop detected
126array(15) {
127  [0]=>
128  string(10) "original.1"
129  [1]=>
130  string(10) "original.2"
131  [2]=>
132  string(10) "original.3"
133  [3]=>
134  string(5) "new.0"
135  [4]=>
136  string(5) "new.1"
137  [5]=>
138  string(5) "new.2"
139  [6]=>
140  string(5) "new.3"
141  [7]=>
142  string(5) "new.4"
143  [8]=>
144  string(5) "new.5"
145  [9]=>
146  string(5) "new.6"
147  [10]=>
148  string(5) "new.7"
149  [11]=>
150  &string(5) "new.8"
151  [12]=>
152  string(5) "new.9"
153  [13]=>
154  string(6) "new.10"
155  [14]=>
156  string(6) "new.11"
157}
158
159Popping elements off an unreferenced array.
160string(10) "original.1"
161string(10) "original.2"
162string(10) "original.3"
163array(0) {
164}
165
166Popping elements off an unreferenced array, using &$value.
167string(10) "original.1"
168string(10) "original.2"
169array(1) {
170  [0]=>
171  string(10) "original.1"
172}
173