]> granicus.if.org Git - php/blob
ef6cf45139
[php] /
1 --TEST--
2 Test array_key_exists() function : usage variations - floats and casting to ints
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 floats as $key argument, then cast float values
13  * to integers and pass as $key argument
14  */
15
16 echo "*** Testing array_key_exists() : usage variations ***\n";
17
18 $keys = array(1.2345678900E-10, 1.00000000000001, 1.99999999999999);
19
20 $search = array ('zero', 'one', 'two');
21
22 $iterator = 1;
23 foreach($keys as $key) {
24     echo "\n-- Iteration $iterator --\n";
25     echo "Pass float as \$key:\n";
26     try {
27         var_dump(array_key_exists($key, $search));
28     } catch (TypeError $exception) {
29         echo $exception->getMessage() . "\n";
30     }
31     echo "Cast float to int:\n";
32     var_dump(array_key_exists((int)$key, $search));
33 }
34
35 echo "Done";
36 ?>
37 --EXPECT--
38 *** Testing array_key_exists() : usage variations ***
39
40 -- Iteration 1 --
41 Pass float as $key:
42 bool(true)
43 Cast float to int:
44 bool(true)
45
46 -- Iteration 1 --
47 Pass float as $key:
48 bool(true)
49 Cast float to int:
50 bool(true)
51
52 -- Iteration 1 --
53 Pass float as $key:
54 bool(true)
55 Cast float to int:
56 bool(true)
57 Done