summaryrefslogtreecommitdiff
path: root/tests/lang/foreachLoop.002.phpt
blob: 31492953a466efe7b33771df53f4031b9d8faa26 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
--TEST--
Foreach loop tests - modifying the array during the loop.
--FILE--
<?php

echo "\nDirectly changing array values.\n";
$a = array("original.1","original.2","original.3");
foreach ($a as $k=>$v) {
	$a[$k]="changed.$k";
	var_dump($v);
}
var_dump($a);

echo "\nModifying the foreach \$value.\n";
$a = array("original.1","original.2","original.3");
foreach ($a as $k=>$v) {
	$v="changed.$k";
}
var_dump($a);


echo "\nModifying the foreach &\$value.\n";
$a = array("original.1","original.2","original.3");
foreach ($a as $k=>&$v) {
	$v="changed.$k";
}
var_dump($a);

echo "\nPushing elements onto an unreferenced array.\n";
$a = array("original.1","original.2","original.3");
$counter=0;
foreach ($a as $v) {
	array_push($a, "new.$counter");

	//avoid infinite loop if test is failing
    if ($counter++>10) {
    	echo "Loop detected\n";
    	break;    	
    }
}
var_dump($a);

echo "\nPushing elements onto an unreferenced array, using &\$value.\n";
$a = array("original.1","original.2","original.3");
$counter=0;
foreach ($a as &$v) {
	array_push($a, "new.$counter");

	//avoid infinite loop if test is failing
    if ($counter++>10) {
    	echo "Loop detected\n";
    	break;    	
    }	
}
var_dump($a);

echo "\nPopping elements off an unreferenced array.\n";
$a = array("original.1","original.2","original.3");
foreach ($a as $v) {
	array_pop($a);
	var_dump($v);	
}
var_dump($a);

echo "\nPopping elements off an unreferenced array, using &\$value.\n";
$a = array("original.1","original.2","original.3");
foreach ($a as &$v) {
	array_pop($a);
	var_dump($v);
}
var_dump($a);

?>
--EXPECT--

Directly changing array values.
string(10) "original.1"
string(10) "original.2"
string(10) "original.3"
array(3) {
  [0]=>
  string(9) "changed.0"
  [1]=>
  string(9) "changed.1"
  [2]=>
  string(9) "changed.2"
}

Modifying the foreach $value.
array(3) {
  [0]=>
  string(10) "original.1"
  [1]=>
  string(10) "original.2"
  [2]=>
  string(10) "original.3"
}

Modifying the foreach &$value.
array(3) {
  [0]=>
  string(9) "changed.0"
  [1]=>
  string(9) "changed.1"
  [2]=>
  &string(9) "changed.2"
}

Pushing elements onto an unreferenced array.
array(6) {
  [0]=>
  string(10) "original.1"
  [1]=>
  string(10) "original.2"
  [2]=>
  string(10) "original.3"
  [3]=>
  string(5) "new.0"
  [4]=>
  string(5) "new.1"
  [5]=>
  string(5) "new.2"
}

Pushing elements onto an unreferenced array, using &$value.
Loop detected
array(15) {
  [0]=>
  string(10) "original.1"
  [1]=>
  string(10) "original.2"
  [2]=>
  string(10) "original.3"
  [3]=>
  string(5) "new.0"
  [4]=>
  string(5) "new.1"
  [5]=>
  string(5) "new.2"
  [6]=>
  string(5) "new.3"
  [7]=>
  string(5) "new.4"
  [8]=>
  string(5) "new.5"
  [9]=>
  string(5) "new.6"
  [10]=>
  string(5) "new.7"
  [11]=>
  &string(5) "new.8"
  [12]=>
  string(5) "new.9"
  [13]=>
  string(6) "new.10"
  [14]=>
  string(6) "new.11"
}

Popping elements off an unreferenced array.
string(10) "original.1"
string(10) "original.2"
string(10) "original.3"
array(0) {
}

Popping elements off an unreferenced array, using &$value.
string(10) "original.1"
string(10) "original.2"
array(1) {
  [0]=>
  string(10) "original.1"
}