1--TEST--
2Test each() function : usage variations - keys of different data types
3--FILE--
4<?php
5/* Prototype  : array each(array $arr)
6 * Description: Return the currently pointed key..value pair in the passed array,
7 * and advance the pointer to the next element
8 * Source code: Zend/zend_builtin_functions.c
9 */
10
11/*
12 * Pass each() arrays where the keys are different data types to test behaviour
13 */
14
15echo "*** Testing each() : usage variations ***\n";
16
17//get an unset variable
18$unset_var = 10;
19unset ($unset_var);
20
21// heredoc string
22$heredoc = <<<EOT
23hello world
24EOT;
25
26// unexpected values to be passed as $arr
27$inputs = array(
28
29       // int data
30/*1*/  'int' => array(
31       0 => 'zero',
32       1 => 'one',
33       12345 => 'positive',
34       -2345 => 'negative',
35       ),
36
37       // float data
38/*2*/  'float' => array(
39       10.5 => 'positive',
40       -10.5 => 'negative',
41       .5 => 'half',
42       ),
43
44/*3*/  'extreme floats' => array(
45       12.3456789000e6 => 'large',
46       12.3456789000E-10 => 'small',
47       ),
48
49       // null data
50/*4*/ 'null uppercase' => array(
51       NULL => 'null 1',
52       ),
53
54/*5*/  'null lowercase' => array(
55       null => 'null 2',
56       ),
57
58       // boolean data
59/*6*/ 'bool lowercase' => array(
60       true => 'lowert',
61       false => 'lowerf',
62       ),
63
64/*7*/  'bool uppercase' => array(
65       TRUE => 'uppert',
66       FALSE => 'upperf',
67       ),
68
69       // empty data
70/*8*/ 'empty double quotes' => array(
71       "" => 'emptyd',
72       ),
73
74/*9*/  'empty single quotes' => array(
75       '' => 'emptys',
76       ),
77
78       // string data
79/*10*/ 'string' => array(
80       "stringd" => 'stringd',
81       'strings' => 'strings',
82       $heredoc => 'stringh',
83       ),
84
85       // undefined data
86/*11*/ 'undefined' => array(
87       @$undefined_var => 'undefined',
88       ),
89
90       // unset data
91/*12*/ 'unset' => array(
92       @$unset_var => 'unset',
93       ),
94);
95
96// loop through each element of $inputs to check the behavior of each()
97$iterator = 1;
98foreach($inputs as $key => $input) {
99  echo "\n-- Iteration $iterator: $key data --\n";
100  var_dump( each($input) );
101  $iterator++;
102};
103
104echo "Done";
105?>
106--EXPECTF--
107*** Testing each() : usage variations ***
108
109-- Iteration 1: int data --
110array(4) {
111  [1]=>
112  string(4) "zero"
113  ["value"]=>
114  string(4) "zero"
115  [0]=>
116  int(0)
117  ["key"]=>
118  int(0)
119}
120
121-- Iteration 2: float data --
122array(4) {
123  [1]=>
124  string(8) "positive"
125  ["value"]=>
126  string(8) "positive"
127  [0]=>
128  int(10)
129  ["key"]=>
130  int(10)
131}
132
133-- Iteration 3: extreme floats data --
134array(4) {
135  [1]=>
136  string(5) "large"
137  ["value"]=>
138  string(5) "large"
139  [0]=>
140  int(12345678)
141  ["key"]=>
142  int(12345678)
143}
144
145-- Iteration 4: null uppercase data --
146array(4) {
147  [1]=>
148  string(6) "null 1"
149  ["value"]=>
150  string(6) "null 1"
151  [0]=>
152  string(0) ""
153  ["key"]=>
154  string(0) ""
155}
156
157-- Iteration 5: null lowercase data --
158array(4) {
159  [1]=>
160  string(6) "null 2"
161  ["value"]=>
162  string(6) "null 2"
163  [0]=>
164  string(0) ""
165  ["key"]=>
166  string(0) ""
167}
168
169-- Iteration 6: bool lowercase data --
170array(4) {
171  [1]=>
172  string(6) "lowert"
173  ["value"]=>
174  string(6) "lowert"
175  [0]=>
176  int(1)
177  ["key"]=>
178  int(1)
179}
180
181-- Iteration 7: bool uppercase data --
182array(4) {
183  [1]=>
184  string(6) "uppert"
185  ["value"]=>
186  string(6) "uppert"
187  [0]=>
188  int(1)
189  ["key"]=>
190  int(1)
191}
192
193-- Iteration 8: empty double quotes data --
194array(4) {
195  [1]=>
196  string(6) "emptyd"
197  ["value"]=>
198  string(6) "emptyd"
199  [0]=>
200  string(0) ""
201  ["key"]=>
202  string(0) ""
203}
204
205-- Iteration 9: empty single quotes data --
206array(4) {
207  [1]=>
208  string(6) "emptys"
209  ["value"]=>
210  string(6) "emptys"
211  [0]=>
212  string(0) ""
213  ["key"]=>
214  string(0) ""
215}
216
217-- Iteration 10: string data --
218array(4) {
219  [1]=>
220  string(7) "stringd"
221  ["value"]=>
222  string(7) "stringd"
223  [0]=>
224  string(7) "stringd"
225  ["key"]=>
226  string(7) "stringd"
227}
228
229-- Iteration 11: undefined data --
230array(4) {
231  [1]=>
232  string(9) "undefined"
233  ["value"]=>
234  string(9) "undefined"
235  [0]=>
236  string(0) ""
237  ["key"]=>
238  string(0) ""
239}
240
241-- Iteration 12: unset data --
242array(4) {
243  [1]=>
244  string(5) "unset"
245  ["value"]=>
246  string(5) "unset"
247  [0]=>
248  string(0) ""
249  ["key"]=>
250  string(0) ""
251}
252Done
253