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--EXPECT-- 113*** Testing array_key_exists() : usage variations *** 114 115-- Iteration 1: int data -- 116-- $key arguments are int data: 117bool(true) 118bool(true) 119bool(true) 120bool(true) 121-- $key arguments are float data: 122bool(false) 123bool(false) 124bool(true) 125-- $key arguments are extreme floats data: 126bool(false) 127bool(true) 128-- $key arguments are null uppercase data: 129bool(false) 130-- $key arguments are null lowercase data: 131bool(false) 132-- $key arguments are bool lowercase data: 133bool(true) 134bool(true) 135-- $key arguments are bool uppercase data: 136bool(true) 137bool(true) 138-- $key arguments are empty double quotes data: 139bool(false) 140-- $key arguments are empty single quotes data: 141bool(false) 142-- $key arguments are string data: 143bool(false) 144bool(false) 145bool(false) 146-- $key arguments are undefined data: 147bool(false) 148-- $key arguments are unset data: 149bool(false) 150 151-- Iteration 2: float data -- 152-- $key arguments are int data: 153bool(true) 154bool(false) 155bool(false) 156bool(false) 157-- $key arguments are float data: 158bool(true) 159bool(true) 160bool(true) 161-- $key arguments are extreme floats data: 162bool(false) 163bool(true) 164-- $key arguments are null uppercase data: 165bool(false) 166-- $key arguments are null lowercase data: 167bool(false) 168-- $key arguments are bool lowercase data: 169bool(false) 170bool(true) 171-- $key arguments are bool uppercase data: 172bool(false) 173bool(true) 174-- $key arguments are empty double quotes data: 175bool(false) 176-- $key arguments are empty single quotes data: 177bool(false) 178-- $key arguments are string data: 179bool(false) 180bool(false) 181bool(false) 182-- $key arguments are undefined data: 183bool(false) 184-- $key arguments are unset data: 185bool(false) 186 187-- Iteration 3: extreme floats data -- 188-- $key arguments are int data: 189bool(true) 190bool(false) 191bool(false) 192bool(false) 193-- $key arguments are float data: 194bool(false) 195bool(false) 196bool(true) 197-- $key arguments are extreme floats data: 198bool(true) 199bool(true) 200-- $key arguments are null uppercase data: 201bool(false) 202-- $key arguments are null lowercase data: 203bool(false) 204-- $key arguments are bool lowercase data: 205bool(false) 206bool(true) 207-- $key arguments are bool uppercase data: 208bool(false) 209bool(true) 210-- $key arguments are empty double quotes data: 211bool(false) 212-- $key arguments are empty single quotes data: 213bool(false) 214-- $key arguments are string data: 215bool(false) 216bool(false) 217bool(false) 218-- $key arguments are undefined data: 219bool(false) 220-- $key arguments are unset data: 221bool(false) 222 223-- Iteration 4: null uppercase data -- 224-- $key arguments are int data: 225bool(false) 226bool(false) 227bool(false) 228bool(false) 229-- $key arguments are float data: 230bool(false) 231bool(false) 232bool(false) 233-- $key arguments are extreme floats data: 234bool(false) 235bool(false) 236-- $key arguments are null uppercase data: 237bool(true) 238-- $key arguments are null lowercase data: 239bool(true) 240-- $key arguments are bool lowercase data: 241bool(false) 242bool(false) 243-- $key arguments are bool uppercase data: 244bool(false) 245bool(false) 246-- $key arguments are empty double quotes data: 247bool(true) 248-- $key arguments are empty single quotes data: 249bool(true) 250-- $key arguments are string data: 251bool(false) 252bool(false) 253bool(false) 254-- $key arguments are undefined data: 255bool(true) 256-- $key arguments are unset data: 257bool(true) 258 259-- Iteration 5: null lowercase data -- 260-- $key arguments are int data: 261bool(false) 262bool(false) 263bool(false) 264bool(false) 265-- $key arguments are float data: 266bool(false) 267bool(false) 268bool(false) 269-- $key arguments are extreme floats data: 270bool(false) 271bool(false) 272-- $key arguments are null uppercase data: 273bool(true) 274-- $key arguments are null lowercase data: 275bool(true) 276-- $key arguments are bool lowercase data: 277bool(false) 278bool(false) 279-- $key arguments are bool uppercase data: 280bool(false) 281bool(false) 282-- $key arguments are empty double quotes data: 283bool(true) 284-- $key arguments are empty single quotes data: 285bool(true) 286-- $key arguments are string data: 287bool(false) 288bool(false) 289bool(false) 290-- $key arguments are undefined data: 291bool(true) 292-- $key arguments are unset data: 293bool(true) 294 295-- Iteration 6: bool lowercase data -- 296-- $key arguments are int data: 297bool(true) 298bool(true) 299bool(false) 300bool(false) 301-- $key arguments are float data: 302bool(false) 303bool(false) 304bool(true) 305-- $key arguments are extreme floats data: 306bool(false) 307bool(true) 308-- $key arguments are null uppercase data: 309bool(false) 310-- $key arguments are null lowercase data: 311bool(false) 312-- $key arguments are bool lowercase data: 313bool(true) 314bool(true) 315-- $key arguments are bool uppercase data: 316bool(true) 317bool(true) 318-- $key arguments are empty double quotes data: 319bool(false) 320-- $key arguments are empty single quotes data: 321bool(false) 322-- $key arguments are string data: 323bool(false) 324bool(false) 325bool(false) 326-- $key arguments are undefined data: 327bool(false) 328-- $key arguments are unset data: 329bool(false) 330 331-- Iteration 7: bool uppercase data -- 332-- $key arguments are int data: 333bool(true) 334bool(true) 335bool(false) 336bool(false) 337-- $key arguments are float data: 338bool(false) 339bool(false) 340bool(true) 341-- $key arguments are extreme floats data: 342bool(false) 343bool(true) 344-- $key arguments are null uppercase data: 345bool(false) 346-- $key arguments are null lowercase data: 347bool(false) 348-- $key arguments are bool lowercase data: 349bool(true) 350bool(true) 351-- $key arguments are bool uppercase data: 352bool(true) 353bool(true) 354-- $key arguments are empty double quotes data: 355bool(false) 356-- $key arguments are empty single quotes data: 357bool(false) 358-- $key arguments are string data: 359bool(false) 360bool(false) 361bool(false) 362-- $key arguments are undefined data: 363bool(false) 364-- $key arguments are unset data: 365bool(false) 366 367-- Iteration 8: empty double quotes data -- 368-- $key arguments are int data: 369bool(false) 370bool(false) 371bool(false) 372bool(false) 373-- $key arguments are float data: 374bool(false) 375bool(false) 376bool(false) 377-- $key arguments are extreme floats data: 378bool(false) 379bool(false) 380-- $key arguments are null uppercase data: 381bool(true) 382-- $key arguments are null lowercase data: 383bool(true) 384-- $key arguments are bool lowercase data: 385bool(false) 386bool(false) 387-- $key arguments are bool uppercase data: 388bool(false) 389bool(false) 390-- $key arguments are empty double quotes data: 391bool(true) 392-- $key arguments are empty single quotes data: 393bool(true) 394-- $key arguments are string data: 395bool(false) 396bool(false) 397bool(false) 398-- $key arguments are undefined data: 399bool(true) 400-- $key arguments are unset data: 401bool(true) 402 403-- Iteration 9: empty single quotes data -- 404-- $key arguments are int data: 405bool(false) 406bool(false) 407bool(false) 408bool(false) 409-- $key arguments are float data: 410bool(false) 411bool(false) 412bool(false) 413-- $key arguments are extreme floats data: 414bool(false) 415bool(false) 416-- $key arguments are null uppercase data: 417bool(true) 418-- $key arguments are null lowercase data: 419bool(true) 420-- $key arguments are bool lowercase data: 421bool(false) 422bool(false) 423-- $key arguments are bool uppercase data: 424bool(false) 425bool(false) 426-- $key arguments are empty double quotes data: 427bool(true) 428-- $key arguments are empty single quotes data: 429bool(true) 430-- $key arguments are string data: 431bool(false) 432bool(false) 433bool(false) 434-- $key arguments are undefined data: 435bool(true) 436-- $key arguments are unset data: 437bool(true) 438 439-- Iteration 10: string data -- 440-- $key arguments are int data: 441bool(false) 442bool(false) 443bool(false) 444bool(false) 445-- $key arguments are float data: 446bool(false) 447bool(false) 448bool(false) 449-- $key arguments are extreme floats data: 450bool(false) 451bool(false) 452-- $key arguments are null uppercase data: 453bool(false) 454-- $key arguments are null lowercase data: 455bool(false) 456-- $key arguments are bool lowercase data: 457bool(false) 458bool(false) 459-- $key arguments are bool uppercase data: 460bool(false) 461bool(false) 462-- $key arguments are empty double quotes data: 463bool(false) 464-- $key arguments are empty single quotes data: 465bool(false) 466-- $key arguments are string data: 467bool(true) 468bool(true) 469bool(true) 470-- $key arguments are undefined data: 471bool(false) 472-- $key arguments are unset data: 473bool(false) 474 475-- Iteration 11: undefined data -- 476-- $key arguments are int data: 477bool(false) 478bool(false) 479bool(false) 480bool(false) 481-- $key arguments are float data: 482bool(false) 483bool(false) 484bool(false) 485-- $key arguments are extreme floats data: 486bool(false) 487bool(false) 488-- $key arguments are null uppercase data: 489bool(true) 490-- $key arguments are null lowercase data: 491bool(true) 492-- $key arguments are bool lowercase data: 493bool(false) 494bool(false) 495-- $key arguments are bool uppercase data: 496bool(false) 497bool(false) 498-- $key arguments are empty double quotes data: 499bool(true) 500-- $key arguments are empty single quotes data: 501bool(true) 502-- $key arguments are string data: 503bool(false) 504bool(false) 505bool(false) 506-- $key arguments are undefined data: 507bool(true) 508-- $key arguments are unset data: 509bool(true) 510 511-- Iteration 12: unset data -- 512-- $key arguments are int data: 513bool(false) 514bool(false) 515bool(false) 516bool(false) 517-- $key arguments are float data: 518bool(false) 519bool(false) 520bool(false) 521-- $key arguments are extreme floats data: 522bool(false) 523bool(false) 524-- $key arguments are null uppercase data: 525bool(true) 526-- $key arguments are null lowercase data: 527bool(true) 528-- $key arguments are bool lowercase data: 529bool(false) 530bool(false) 531-- $key arguments are bool uppercase data: 532bool(false) 533bool(false) 534-- $key arguments are empty double quotes data: 535bool(true) 536-- $key arguments are empty single quotes data: 537bool(true) 538-- $key arguments are string data: 539bool(false) 540bool(false) 541bool(false) 542-- $key arguments are undefined data: 543bool(true) 544-- $key arguments are unset data: 545bool(true) 546Done 547