1--TEST--
2Test array_key_exists() function : usage variations - Pass differnt 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
102--EXPECTF--
103*** Testing array_key_exists() : usage variations ***
104
105-- Iteration 1 --
106
107Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
108NULL
109
110-- Iteration 2 --
111
112Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
113NULL
114
115-- Iteration 3 --
116
117Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
118NULL
119
120-- Iteration 4 --
121
122Warning: array_key_exists() expects parameter 2 to be array, integer given in %s on line %d
123NULL
124
125-- Iteration 5 --
126
127Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
128NULL
129
130-- Iteration 6 --
131
132Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
133NULL
134
135-- Iteration 7 --
136
137Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
138NULL
139
140-- Iteration 8 --
141
142Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
143NULL
144
145-- Iteration 9 --
146
147Warning: array_key_exists() expects parameter 2 to be array, double given in %s on line %d
148NULL
149
150-- Iteration 10 --
151
152Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
153NULL
154
155-- Iteration 11 --
156
157Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
158NULL
159
160-- Iteration 12 --
161
162Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
163NULL
164
165-- Iteration 13 --
166
167Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
168NULL
169
170-- Iteration 14 --
171
172Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
173NULL
174
175-- Iteration 15 --
176
177Warning: array_key_exists() expects parameter 2 to be array, boolean given in %s on line %d
178NULL
179
180-- Iteration 16 --
181
182Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
183NULL
184
185-- Iteration 17 --
186
187Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
188NULL
189
190-- Iteration 18 --
191bool(false)
192
193-- Iteration 19 --
194
195Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
196NULL
197
198-- Iteration 20 --
199
200Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
201NULL
202
203-- Iteration 21 --
204
205Warning: array_key_exists() expects parameter 2 to be array, string given in %s on line %d
206NULL
207
208-- Iteration 22 --
209bool(false)
210
211-- Iteration 23 --
212
213Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
214NULL
215
216-- Iteration 24 --
217
218Warning: array_key_exists() expects parameter 2 to be array, null given in %s on line %d
219NULL
220
221-- Iteration 25 --
222
223Warning: array_key_exists() expects parameter 2 to be array, resource given in %s on line %d
224NULL
225Done
226