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
102--EXPECTF--
103*** Testing array_key_exists() : usage variations ***
104
105-- Iteration 1 --
106bool(true)
107
108-- Iteration 2 --
109bool(true)
110
111-- Iteration 3 --
112bool(false)
113
114-- Iteration 4 --
115bool(false)
116
117-- Iteration 5 --
118
119Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
120bool(false)
121
122-- Iteration 6 --
123
124Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
125bool(false)
126
127-- Iteration 7 --
128
129Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
130bool(false)
131
132-- Iteration 8 --
133
134Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
135bool(false)
136
137-- Iteration 9 --
138
139Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
140bool(false)
141
142-- Iteration 10 --
143bool(false)
144
145-- Iteration 11 --
146bool(false)
147
148-- Iteration 12 --
149
150Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
151bool(false)
152
153-- Iteration 13 --
154
155Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
156bool(false)
157
158-- Iteration 14 --
159
160Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
161bool(false)
162
163-- Iteration 15 --
164
165Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
166bool(false)
167
168-- Iteration 16 --
169bool(false)
170
171-- Iteration 17 --
172bool(false)
173
174-- Iteration 18 --
175
176Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
177bool(false)
178
179-- Iteration 19 --
180bool(true)
181
182-- Iteration 20 --
183bool(true)
184
185-- Iteration 21 --
186bool(true)
187
188-- Iteration 22 --
189
190Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
191bool(false)
192
193-- Iteration 23 --
194bool(false)
195
196-- Iteration 24 --
197bool(false)
198
199-- Iteration 25 --
200
201Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
202bool(false)
203Done