1--TEST--
2Test array_key_exists() function : usage variations - Pass different data types to $search arg
3--FILE--
4<?php
5/* Prototype  : bool array_key_exists(mixed $key, array $search)
6 * Description: Checks if the given key or index exists in the array
7 * Source code: ext/standard/array.c
8 * Alias to functions: key_exists
9 */
10
11/*
12 * Pass different data types as $search argument to array_key_exists() to test behaviour
13 */
14
15echo "*** Testing array_key_exists() : usage variations ***\n";
16
17// Initialise function arguments not being substituted
18$key = 'val';
19
20//get an unset variable
21$unset_var = 10;
22unset ($unset_var);
23
24// get a class
25class classA
26{
27  public function __toString() {
28    return "Class A object";
29  }
30}
31
32// heredoc string
33$heredoc = <<<EOT
34hello world
35EOT;
36
37// get a resource variable
38$fp = fopen(__FILE__, "r");
39
40// unexpected values to be passed to $search argument
41$inputs = array(
42
43       // int data
44/*1*/  0,
45       1,
46       12345,
47       -2345,
48
49       // float data
50/*5*/  10.5,
51       -10.5,
52       12.3456789000e10,
53       12.3456789000E-10,
54       .5,
55
56       // null data
57/*10*/ NULL,
58       null,
59
60       // boolean data
61/*12*/ true,
62       false,
63       TRUE,
64       FALSE,
65
66       // empty data
67/*16*/ "",
68       '',
69       array(),
70
71       // string data
72/*19*/ "string",
73       'string',
74       $heredoc,
75
76       // object data
77/*22*/ new classA(),
78
79       // undefined data
80/*23*/ @$undefined_var,
81
82       // unset data
83/*24*/ @$unset_var,
84
85       // resource variable
86/*25*/ $fp
87);
88
89// loop through each element of $inputs to check the behavior of array_key_exists()
90$iterator = 1;
91foreach($inputs as $input) {
92  echo "\n-- Iteration $iterator --\n";
93  var_dump( array_key_exists($key, $input) );
94  $iterator++;
95};
96
97fclose($fp);
98
99echo "Done";
100?>
101--EXPECTF--
102*** Testing array_key_exists() : usage variations ***
103
104-- Iteration 1 --
105
106Warning: array_key_exists() expects parameter 2 to be array, int given in %s on line %d
107NULL
108
109-- Iteration 2 --
110
111Warning: array_key_exists() expects parameter 2 to be array, int given in %s on line %d
112NULL
113
114-- Iteration 3 --
115
116Warning: array_key_exists() expects parameter 2 to be array, int given in %s on line %d
117NULL
118
119-- Iteration 4 --
120
121Warning: array_key_exists() expects parameter 2 to be array, int given in %s on line %d
122NULL
123
124-- Iteration 5 --
125
126Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
127NULL
128
129-- Iteration 6 --
130
131Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
132NULL
133
134-- Iteration 7 --
135
136Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
137NULL
138
139-- Iteration 8 --
140
141Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
142NULL
143
144-- Iteration 9 --
145
146Warning: array_key_exists() expects parameter 2 to be array, float given in %s on line %d
147NULL
148
149-- Iteration 10 --
150
151Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
152NULL
153
154-- Iteration 11 --
155
156Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
157NULL
158
159-- Iteration 12 --
160
161Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
162NULL
163
164-- Iteration 13 --
165
166Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
167NULL
168
169-- Iteration 14 --
170
171Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
172NULL
173
174-- Iteration 15 --
175
176Warning: array_key_exists() expects parameter 2 to be array, bool given in %s on line %d
177NULL
178
179-- Iteration 16 --
180
181Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
182NULL
183
184-- Iteration 17 --
185
186Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
187NULL
188
189-- Iteration 18 --
190bool(false)
191
192-- Iteration 19 --
193
194Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
195NULL
196
197-- Iteration 20 --
198
199Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
200NULL
201
202-- Iteration 21 --
203
204Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
205NULL
206
207-- Iteration 22 --
208bool(false)
209
210-- Iteration 23 --
211
212Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
213NULL
214
215-- Iteration 24 --
216
217Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
218NULL
219
220-- Iteration 25 --
221
222Warning: array_key_exists() expects parameter 2 to be array, resource given in %s on line %d
223NULL
224Done
225