1--TEST-- 2Test key() function : usage variations - Pass different data types as $array_arg arg. 3--FILE-- 4<?php 5/* Prototype : mixed key(array $array_arg) 6 * Description: Return the key of the element currently pointed to by the internal array pointer 7 * Source code: ext/standard/array.c 8 */ 9 10/* 11 * Pass different data types as $array_arg argument to test behaviour of key() 12 */ 13 14echo "*** Testing key() : usage variations ***\n"; 15 16//get an unset variable 17$unset_var = 10; 18unset ($unset_var); 19 20// get a class 21class classA 22{ 23 var $var1; 24 public function __toString() { 25 return "Class A object"; 26 } 27} 28 29// heredoc string 30$heredoc = <<<EOT 31hello world 32EOT; 33 34// get a resource variable 35$fp = fopen(__FILE__, "r"); 36 37// unexpected values to be passed to $array_arg argument 38$inputs = array( 39 40 // int data 41/*1*/ 0, 42 1, 43 12345, 44 -2345, 45 46 // float data 47/*5*/ 10.5, 48 -10.5, 49 12.3456789000e10, 50 12.3456789000E-10, 51 .5, 52 53 // null data 54/*10*/ NULL, 55 null, 56 57 // boolean data 58/*12*/ true, 59 false, 60 TRUE, 61 FALSE, 62 63 // empty data 64/*16*/ "", 65 '', 66 array(), 67 68 // string data 69/*19*/ "string", 70 'string', 71 $heredoc, 72 73 // object data 74/*22*/ new classA(), 75 76 // undefined data 77/*23*/ @$undefined_var, 78 79 // unset data 80/*24*/ @$unset_var, 81 82 // resource variable 83/*25*/ $fp 84); 85 86// loop through each element of $inputs to check the behavior of key() 87$iterator = 1; 88foreach($inputs as $input) { 89 echo "\n-- Iteration $iterator --\n"; 90 var_dump( key($input) ); 91 $iterator++; 92}; 93 94fclose($fp); 95?> 96===DONE=== 97--EXPECTF-- 98*** Testing key() : usage variations *** 99 100-- Iteration 1 -- 101 102Warning: key() expects parameter 1 to be array, int given in %s on line %d 103NULL 104 105-- Iteration 2 -- 106 107Warning: key() expects parameter 1 to be array, int given in %s on line %d 108NULL 109 110-- Iteration 3 -- 111 112Warning: key() expects parameter 1 to be array, int given in %s on line %d 113NULL 114 115-- Iteration 4 -- 116 117Warning: key() expects parameter 1 to be array, int given in %s on line %d 118NULL 119 120-- Iteration 5 -- 121 122Warning: key() expects parameter 1 to be array, float given in %s on line %d 123NULL 124 125-- Iteration 6 -- 126 127Warning: key() expects parameter 1 to be array, float given in %s on line %d 128NULL 129 130-- Iteration 7 -- 131 132Warning: key() expects parameter 1 to be array, float given in %s on line %d 133NULL 134 135-- Iteration 8 -- 136 137Warning: key() expects parameter 1 to be array, float given in %s on line %d 138NULL 139 140-- Iteration 9 -- 141 142Warning: key() expects parameter 1 to be array, float given in %s on line %d 143NULL 144 145-- Iteration 10 -- 146 147Warning: key() expects parameter 1 to be array, null given in %s on line %d 148NULL 149 150-- Iteration 11 -- 151 152Warning: key() expects parameter 1 to be array, null given in %s on line %d 153NULL 154 155-- Iteration 12 -- 156 157Warning: key() expects parameter 1 to be array, bool given in %s on line %d 158NULL 159 160-- Iteration 13 -- 161 162Warning: key() expects parameter 1 to be array, bool given in %s on line %d 163NULL 164 165-- Iteration 14 -- 166 167Warning: key() expects parameter 1 to be array, bool given in %s on line %d 168NULL 169 170-- Iteration 15 -- 171 172Warning: key() expects parameter 1 to be array, bool given in %s on line %d 173NULL 174 175-- Iteration 16 -- 176 177Warning: key() expects parameter 1 to be array, string given in %s on line %d 178NULL 179 180-- Iteration 17 -- 181 182Warning: key() expects parameter 1 to be array, string given in %s on line %d 183NULL 184 185-- Iteration 18 -- 186NULL 187 188-- Iteration 19 -- 189 190Warning: key() expects parameter 1 to be array, string given in %s on line %d 191NULL 192 193-- Iteration 20 -- 194 195Warning: key() expects parameter 1 to be array, string given in %s on line %d 196NULL 197 198-- Iteration 21 -- 199 200Warning: key() expects parameter 1 to be array, string given in %s on line %d 201NULL 202 203-- Iteration 22 -- 204string(4) "var1" 205 206-- Iteration 23 -- 207 208Warning: key() expects parameter 1 to be array, null given in %s on line %d 209NULL 210 211-- Iteration 24 -- 212 213Warning: key() expects parameter 1 to be array, null given in %s on line %d 214NULL 215 216-- Iteration 25 -- 217 218Warning: key() expects parameter 1 to be array, resource given in %s on line %d 219NULL 220===DONE=== 221