1--TEST--
2Test array_key_exists() function : usage variations - Pass different data types as $key arg
3--FILE--
4<?php
5/*
6 * Pass different data types as $key argument to array_key_exists() to test behaviour
7 */
8
9echo "*** Testing array_key_exists() : usage variations ***\n";
10
11// Initialise function arguments not being substituted
12$search = array ('zero', 'key' => 'val', 'two', 10 => 'value');
13
14//get an unset variable
15$unset_var = 10;
16unset ($unset_var);
17
18// get a class
19class classA
20{
21  public function __toString() {
22    return "key";
23  }
24}
25
26// heredoc string
27$heredoc = <<<EOT
28key
29EOT;
30
31// get a resource variable
32$fp = fopen(__FILE__, "r");
33
34// unexpected values to be passed to $key argument
35$inputs = array(
36
37       // int data
38/*1*/  0,
39       1,
40       12345,
41       -2345,
42
43       // null data
44/*10*/ NULL,
45       null,
46
47       // boolean data
48/*12*/ true,
49       false,
50       TRUE,
51       FALSE,
52
53       // empty data
54/*16*/ "",
55       '',
56       array(),
57
58       // string data
59/*19*/ "key",
60       'key',
61       $heredoc,
62
63       // object data
64/*22*/ new classA(),
65
66       // undefined data
67/*23*/ @$undefined_var,
68
69       // unset data
70/*24*/ @$unset_var,
71
72       // resource variable
73/*25*/ $fp
74);
75
76// loop through each element of $inputs to check the behavior of array_key_exists()
77$iterator = 1;
78foreach($inputs as $input) {
79  echo "\n-- Iteration $iterator --\n";
80  try {
81      var_dump( array_key_exists($input, $search) );
82  } catch (TypeError $exception) {
83      echo $exception->getMessage() . "\n";
84  }
85  $iterator++;
86};
87
88fclose($fp);
89
90echo "Done";
91?>
92--EXPECTF--
93*** Testing array_key_exists() : usage variations ***
94
95-- Iteration 1 --
96bool(true)
97
98-- Iteration 2 --
99bool(true)
100
101-- Iteration 3 --
102bool(false)
103
104-- Iteration 4 --
105bool(false)
106
107-- Iteration 5 --
108bool(false)
109
110-- Iteration 6 --
111bool(false)
112
113-- Iteration 7 --
114bool(true)
115
116-- Iteration 8 --
117bool(true)
118
119-- Iteration 9 --
120bool(true)
121
122-- Iteration 10 --
123bool(true)
124
125-- Iteration 11 --
126bool(false)
127
128-- Iteration 12 --
129bool(false)
130
131-- Iteration 13 --
132Cannot access offset of type array on array
133
134-- Iteration 14 --
135bool(true)
136
137-- Iteration 15 --
138bool(true)
139
140-- Iteration 16 --
141bool(true)
142
143-- Iteration 17 --
144Cannot access offset of type classA on array
145
146-- Iteration 18 --
147bool(false)
148
149-- Iteration 19 --
150bool(false)
151
152-- Iteration 20 --
153
154Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
155bool(false)
156Done
157