Annotation of embedaddon/php/ext/standard/tests/array/array_key_exists_variation1.phpt, revision 1.1.1.1
1.1 misho 1: --TEST--
2: Test 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:
15: echo "*** 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;
22: unset ($unset_var);
23:
24: // get a class
25: class classA
26: {
27: public function __toString() {
28: return "key";
29: }
30: }
31:
32: // heredoc string
33: $heredoc = <<<EOT
34: key
35: EOT;
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;
91: foreach($inputs as $input) {
92: echo "\n-- Iteration $iterator --\n";
93: var_dump( array_key_exists($input, $search) );
94: $iterator++;
95: };
96:
97: fclose($fp);
98:
99: echo "Done";
100: ?>
101:
102: --EXPECTF--
103: *** Testing array_key_exists() : usage variations ***
104:
105: -- Iteration 1 --
106: bool(true)
107:
108: -- Iteration 2 --
109: bool(true)
110:
111: -- Iteration 3 --
112: bool(false)
113:
114: -- Iteration 4 --
115: bool(false)
116:
117: -- Iteration 5 --
118:
119: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
120: bool(false)
121:
122: -- Iteration 6 --
123:
124: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
125: bool(false)
126:
127: -- Iteration 7 --
128:
129: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
130: bool(false)
131:
132: -- Iteration 8 --
133:
134: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
135: bool(false)
136:
137: -- Iteration 9 --
138:
139: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
140: bool(false)
141:
142: -- Iteration 10 --
143: bool(false)
144:
145: -- Iteration 11 --
146: bool(false)
147:
148: -- Iteration 12 --
149:
150: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
151: bool(false)
152:
153: -- Iteration 13 --
154:
155: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
156: bool(false)
157:
158: -- Iteration 14 --
159:
160: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
161: bool(false)
162:
163: -- Iteration 15 --
164:
165: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
166: bool(false)
167:
168: -- Iteration 16 --
169: bool(false)
170:
171: -- Iteration 17 --
172: bool(false)
173:
174: -- Iteration 18 --
175:
176: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
177: bool(false)
178:
179: -- Iteration 19 --
180: bool(true)
181:
182: -- Iteration 20 --
183: bool(true)
184:
185: -- Iteration 21 --
186: bool(true)
187:
188: -- Iteration 22 --
189:
190: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
191: bool(false)
192:
193: -- Iteration 23 --
194: bool(false)
195:
196: -- Iteration 24 --
197: bool(false)
198:
199: -- Iteration 25 --
200:
201: Warning: array_key_exists(): The first argument should be either a string or an integer in %s on line %d
202: bool(false)
203: Done
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>