1--TEST--
2Test array_key_exists() function : usage variations - array keys are different data types
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 an array where the keys are different data types as the $search argument
13 * then pass many different data types as $key argument to test where array_key_exist()
14 * returns true.
15 */
16
17echo "*** Testing array_key_exists() : usage variations ***\n";
18
19//get an unset variable
20$unset_var = 10;
21unset ($unset_var);
22
23// heredoc string
24$heredoc = <<<EOT
25string
26EOT;
27
28// different data types to be iterated over
29$inputs = array(
30
31       // int data
32/*1*/  'int' => array(
33       0 => 'zero',
34       1 => 'one',
35       12345 => 'positive',
36       -2345 => 'negative',
37       ),
38
39       // float data
40/*2*/  'float' => array(
41       10.5 => 'positive',
42       -10.5 => 'negative',
43       .5 => 'half',
44       ),
45
46       'extreme floats' => array(
47       12.3456789000e10 => 'large',
48       12.3456789000E-10 => 'small',
49       ),
50
51       // null data
52/*3*/ 'null uppercase' => array(
53       NULL => 'null 1',
54       ),
55       'null lowercase' => array(
56       null => 'null 2',
57       ),
58
59       // boolean data
60/*4*/ 'bool lowercase' => array(
61       true => 'lowert',
62       false => 'lowerf',
63       ),
64       'bool uppercase' => array(
65       TRUE => 'uppert',
66       FALSE => 'upperf',
67       ),
68
69       // empty data
70/*5*/ 'empty double quotes' => array(
71       "" => 'emptyd',
72       ),
73       'empty single quotes' => array(
74       '' => 'emptys',
75       ),
76
77       // string data
78/*6*/ 'string' => array(
79       "stringd" => 'stringd',
80       'strings' => 'strings',
81       $heredoc => 'stringh',
82       ),
83
84       // undefined data
85/*8*/ 'undefined' => array(
86       @$undefined_var => 'undefined',
87       ),
88
89       // unset data
90/*9*/ 'unset' => array(
91       @$unset_var => 'unset',
92       ),
93);
94
95// loop through each element of $inputs to check the behavior of array_key_exists()
96$iterator = 1;
97foreach($inputs as $type => $input) {
98	echo "\n-- Iteration $iterator: $type data --\n";
99
100	//iterate over again to get all different key values
101	foreach ($inputs as $new_type => $new_input) {
102		echo "-- \$key arguments are $new_type data:\n";
103		foreach ($new_input as $key => $search) {
104			var_dump(array_key_exists($key, $input));
105		}
106	}
107	$iterator++;
108};
109
110echo "Done";
111?>
112
113--EXPECTF--
114*** Testing array_key_exists() : usage variations ***
115
116-- Iteration 1: int data --
117-- $key arguments are int data:
118bool(true)
119bool(true)
120bool(true)
121bool(true)
122-- $key arguments are float data:
123bool(false)
124bool(false)
125bool(true)
126-- $key arguments are extreme floats data:
127bool(false)
128bool(true)
129-- $key arguments are null uppercase data:
130bool(false)
131-- $key arguments are null lowercase data:
132bool(false)
133-- $key arguments are bool lowercase data:
134bool(true)
135bool(true)
136-- $key arguments are bool uppercase data:
137bool(true)
138bool(true)
139-- $key arguments are empty double quotes data:
140bool(false)
141-- $key arguments are empty single quotes data:
142bool(false)
143-- $key arguments are string data:
144bool(false)
145bool(false)
146bool(false)
147-- $key arguments are undefined data:
148bool(false)
149-- $key arguments are unset data:
150bool(false)
151
152-- Iteration 2: float data --
153-- $key arguments are int data:
154bool(true)
155bool(false)
156bool(false)
157bool(false)
158-- $key arguments are float data:
159bool(true)
160bool(true)
161bool(true)
162-- $key arguments are extreme floats data:
163bool(false)
164bool(true)
165-- $key arguments are null uppercase data:
166bool(false)
167-- $key arguments are null lowercase data:
168bool(false)
169-- $key arguments are bool lowercase data:
170bool(false)
171bool(true)
172-- $key arguments are bool uppercase data:
173bool(false)
174bool(true)
175-- $key arguments are empty double quotes data:
176bool(false)
177-- $key arguments are empty single quotes data:
178bool(false)
179-- $key arguments are string data:
180bool(false)
181bool(false)
182bool(false)
183-- $key arguments are undefined data:
184bool(false)
185-- $key arguments are unset data:
186bool(false)
187
188-- Iteration 3: extreme floats data --
189-- $key arguments are int data:
190bool(true)
191bool(false)
192bool(false)
193bool(false)
194-- $key arguments are float data:
195bool(false)
196bool(false)
197bool(true)
198-- $key arguments are extreme floats data:
199bool(true)
200bool(true)
201-- $key arguments are null uppercase data:
202bool(false)
203-- $key arguments are null lowercase data:
204bool(false)
205-- $key arguments are bool lowercase data:
206bool(false)
207bool(true)
208-- $key arguments are bool uppercase data:
209bool(false)
210bool(true)
211-- $key arguments are empty double quotes data:
212bool(false)
213-- $key arguments are empty single quotes data:
214bool(false)
215-- $key arguments are string data:
216bool(false)
217bool(false)
218bool(false)
219-- $key arguments are undefined data:
220bool(false)
221-- $key arguments are unset data:
222bool(false)
223
224-- Iteration 4: null uppercase data --
225-- $key arguments are int data:
226bool(false)
227bool(false)
228bool(false)
229bool(false)
230-- $key arguments are float data:
231bool(false)
232bool(false)
233bool(false)
234-- $key arguments are extreme floats data:
235bool(false)
236bool(false)
237-- $key arguments are null uppercase data:
238bool(true)
239-- $key arguments are null lowercase data:
240bool(true)
241-- $key arguments are bool lowercase data:
242bool(false)
243bool(false)
244-- $key arguments are bool uppercase data:
245bool(false)
246bool(false)
247-- $key arguments are empty double quotes data:
248bool(true)
249-- $key arguments are empty single quotes data:
250bool(true)
251-- $key arguments are string data:
252bool(false)
253bool(false)
254bool(false)
255-- $key arguments are undefined data:
256bool(true)
257-- $key arguments are unset data:
258bool(true)
259
260-- Iteration 5: null lowercase data --
261-- $key arguments are int data:
262bool(false)
263bool(false)
264bool(false)
265bool(false)
266-- $key arguments are float data:
267bool(false)
268bool(false)
269bool(false)
270-- $key arguments are extreme floats data:
271bool(false)
272bool(false)
273-- $key arguments are null uppercase data:
274bool(true)
275-- $key arguments are null lowercase data:
276bool(true)
277-- $key arguments are bool lowercase data:
278bool(false)
279bool(false)
280-- $key arguments are bool uppercase data:
281bool(false)
282bool(false)
283-- $key arguments are empty double quotes data:
284bool(true)
285-- $key arguments are empty single quotes data:
286bool(true)
287-- $key arguments are string data:
288bool(false)
289bool(false)
290bool(false)
291-- $key arguments are undefined data:
292bool(true)
293-- $key arguments are unset data:
294bool(true)
295
296-- Iteration 6: bool lowercase data --
297-- $key arguments are int data:
298bool(true)
299bool(true)
300bool(false)
301bool(false)
302-- $key arguments are float data:
303bool(false)
304bool(false)
305bool(true)
306-- $key arguments are extreme floats data:
307bool(false)
308bool(true)
309-- $key arguments are null uppercase data:
310bool(false)
311-- $key arguments are null lowercase data:
312bool(false)
313-- $key arguments are bool lowercase data:
314bool(true)
315bool(true)
316-- $key arguments are bool uppercase data:
317bool(true)
318bool(true)
319-- $key arguments are empty double quotes data:
320bool(false)
321-- $key arguments are empty single quotes data:
322bool(false)
323-- $key arguments are string data:
324bool(false)
325bool(false)
326bool(false)
327-- $key arguments are undefined data:
328bool(false)
329-- $key arguments are unset data:
330bool(false)
331
332-- Iteration 7: bool uppercase data --
333-- $key arguments are int data:
334bool(true)
335bool(true)
336bool(false)
337bool(false)
338-- $key arguments are float data:
339bool(false)
340bool(false)
341bool(true)
342-- $key arguments are extreme floats data:
343bool(false)
344bool(true)
345-- $key arguments are null uppercase data:
346bool(false)
347-- $key arguments are null lowercase data:
348bool(false)
349-- $key arguments are bool lowercase data:
350bool(true)
351bool(true)
352-- $key arguments are bool uppercase data:
353bool(true)
354bool(true)
355-- $key arguments are empty double quotes data:
356bool(false)
357-- $key arguments are empty single quotes data:
358bool(false)
359-- $key arguments are string data:
360bool(false)
361bool(false)
362bool(false)
363-- $key arguments are undefined data:
364bool(false)
365-- $key arguments are unset data:
366bool(false)
367
368-- Iteration 8: empty double quotes data --
369-- $key arguments are int data:
370bool(false)
371bool(false)
372bool(false)
373bool(false)
374-- $key arguments are float data:
375bool(false)
376bool(false)
377bool(false)
378-- $key arguments are extreme floats data:
379bool(false)
380bool(false)
381-- $key arguments are null uppercase data:
382bool(true)
383-- $key arguments are null lowercase data:
384bool(true)
385-- $key arguments are bool lowercase data:
386bool(false)
387bool(false)
388-- $key arguments are bool uppercase data:
389bool(false)
390bool(false)
391-- $key arguments are empty double quotes data:
392bool(true)
393-- $key arguments are empty single quotes data:
394bool(true)
395-- $key arguments are string data:
396bool(false)
397bool(false)
398bool(false)
399-- $key arguments are undefined data:
400bool(true)
401-- $key arguments are unset data:
402bool(true)
403
404-- Iteration 9: empty single quotes data --
405-- $key arguments are int data:
406bool(false)
407bool(false)
408bool(false)
409bool(false)
410-- $key arguments are float data:
411bool(false)
412bool(false)
413bool(false)
414-- $key arguments are extreme floats data:
415bool(false)
416bool(false)
417-- $key arguments are null uppercase data:
418bool(true)
419-- $key arguments are null lowercase data:
420bool(true)
421-- $key arguments are bool lowercase data:
422bool(false)
423bool(false)
424-- $key arguments are bool uppercase data:
425bool(false)
426bool(false)
427-- $key arguments are empty double quotes data:
428bool(true)
429-- $key arguments are empty single quotes data:
430bool(true)
431-- $key arguments are string data:
432bool(false)
433bool(false)
434bool(false)
435-- $key arguments are undefined data:
436bool(true)
437-- $key arguments are unset data:
438bool(true)
439
440-- Iteration 10: string data --
441-- $key arguments are int data:
442bool(false)
443bool(false)
444bool(false)
445bool(false)
446-- $key arguments are float data:
447bool(false)
448bool(false)
449bool(false)
450-- $key arguments are extreme floats data:
451bool(false)
452bool(false)
453-- $key arguments are null uppercase data:
454bool(false)
455-- $key arguments are null lowercase data:
456bool(false)
457-- $key arguments are bool lowercase data:
458bool(false)
459bool(false)
460-- $key arguments are bool uppercase data:
461bool(false)
462bool(false)
463-- $key arguments are empty double quotes data:
464bool(false)
465-- $key arguments are empty single quotes data:
466bool(false)
467-- $key arguments are string data:
468bool(true)
469bool(true)
470bool(true)
471-- $key arguments are undefined data:
472bool(false)
473-- $key arguments are unset data:
474bool(false)
475
476-- Iteration 11: undefined data --
477-- $key arguments are int data:
478bool(false)
479bool(false)
480bool(false)
481bool(false)
482-- $key arguments are float data:
483bool(false)
484bool(false)
485bool(false)
486-- $key arguments are extreme floats data:
487bool(false)
488bool(false)
489-- $key arguments are null uppercase data:
490bool(true)
491-- $key arguments are null lowercase data:
492bool(true)
493-- $key arguments are bool lowercase data:
494bool(false)
495bool(false)
496-- $key arguments are bool uppercase data:
497bool(false)
498bool(false)
499-- $key arguments are empty double quotes data:
500bool(true)
501-- $key arguments are empty single quotes data:
502bool(true)
503-- $key arguments are string data:
504bool(false)
505bool(false)
506bool(false)
507-- $key arguments are undefined data:
508bool(true)
509-- $key arguments are unset data:
510bool(true)
511
512-- Iteration 12: unset data --
513-- $key arguments are int data:
514bool(false)
515bool(false)
516bool(false)
517bool(false)
518-- $key arguments are float data:
519bool(false)
520bool(false)
521bool(false)
522-- $key arguments are extreme floats data:
523bool(false)
524bool(false)
525-- $key arguments are null uppercase data:
526bool(true)
527-- $key arguments are null lowercase data:
528bool(true)
529-- $key arguments are bool lowercase data:
530bool(false)
531bool(false)
532-- $key arguments are bool uppercase data:
533bool(false)
534bool(false)
535-- $key arguments are empty double quotes data:
536bool(true)
537-- $key arguments are empty single quotes data:
538bool(true)
539-- $key arguments are string data:
540bool(false)
541bool(false)
542bool(false)
543-- $key arguments are undefined data:
544bool(true)
545-- $key arguments are unset data:
546bool(true)
547Done