1--TEST--
2Test array_key_exists() function : usage variations - Pass different data types as $key 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 $key 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$search = array ('zero', 'key' => 'val', 'two');
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 "key";
29  }
30}
31
32// heredoc string
33$heredoc = <<<EOT
34key
35EOT;
36
37// get a resource variable
38$fp = fopen(__FILE__, "r");
39
40// unexpected values to be passed to $key 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*/ "key",
73       'key',
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($input, $search) );
94  $iterator++;
95};
96
97fclose($fp);
98
99echo "Done";
100?>
101--EXPECTF--
102*** Testing array_key_exists() : usage variations ***
103
104-- Iteration 1 --
105bool(true)
106
107-- Iteration 2 --
108bool(true)
109
110-- Iteration 3 --
111bool(false)
112
113-- Iteration 4 --
114bool(false)
115
116-- Iteration 5 --
117
118Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
119bool(false)
120
121-- Iteration 6 --
122
123Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
124bool(false)
125
126-- Iteration 7 --
127
128Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
129bool(false)
130
131-- Iteration 8 --
132
133Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
134bool(false)
135
136-- Iteration 9 --
137
138Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
139bool(false)
140
141-- Iteration 10 --
142bool(false)
143
144-- Iteration 11 --
145bool(false)
146
147-- Iteration 12 --
148
149Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
150bool(false)
151
152-- Iteration 13 --
153
154Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
155bool(false)
156
157-- Iteration 14 --
158
159Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
160bool(false)
161
162-- Iteration 15 --
163
164Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
165bool(false)
166
167-- Iteration 16 --
168bool(false)
169
170-- Iteration 17 --
171bool(false)
172
173-- Iteration 18 --
174
175Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
176bool(false)
177
178-- Iteration 19 --
179bool(true)
180
181-- Iteration 20 --
182bool(true)
183
184-- Iteration 21 --
185bool(true)
186
187-- Iteration 22 --
188
189Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
190bool(false)
191
192-- Iteration 23 --
193bool(false)
194
195-- Iteration 24 --
196bool(false)
197
198-- Iteration 25 --
199
200Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
201bool(false)
202Done
203