key_variation2.phpt 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. --TEST--
  2. Test key() function : usage variations
  3. --FILE--
  4. <?php
  5. /*
  6. * Pass arrays where keys are different data types as $array_arg to key() to test behaviour
  7. */
  8. echo "*** Testing key() : usage variations ***\n";
  9. //get an unset variable
  10. $unset_var = 10;
  11. unset ($unset_var);
  12. // heredoc string
  13. $heredoc = <<<EOT
  14. hello world
  15. EOT;
  16. // unexpected values to be passed as $array_arg
  17. $inputs = array(
  18. // int data
  19. /*1*/ 'int' => array(
  20. 0 => 'zero',
  21. 1 => 'one',
  22. 12345 => 'positive',
  23. -2345 => 'negative',
  24. ),
  25. // null data
  26. /*4*/ 'null uppercase' => array(
  27. NULL => 'null 1',
  28. ),
  29. /*5*/ 'null lowercase' => array(
  30. null => 'null 2',
  31. ),
  32. // boolean data
  33. /*6*/ 'bool lowercase' => array(
  34. true => 'lowert',
  35. false => 'lowerf',
  36. ),
  37. /*7*/ 'bool uppercase' => array(
  38. TRUE => 'uppert',
  39. FALSE => 'upperf',
  40. ),
  41. // empty data
  42. /*8*/ 'empty double quotes' => array(
  43. "" => 'emptyd',
  44. ),
  45. /*9*/ 'empty single quotes' => array(
  46. '' => 'emptys',
  47. ),
  48. // string data
  49. /*10*/ 'string' => array(
  50. "stringd" => 'stringd',
  51. 'strings' => 'strings',
  52. $heredoc => 'stringh',
  53. ),
  54. // undefined data
  55. /*11*/ 'undefined' => array(
  56. @$undefined_var => 'undefined',
  57. ),
  58. // unset data
  59. /*12*/ 'unset' => array(
  60. @$unset_var => 'unset',
  61. ),
  62. );
  63. // loop through each element of $inputs to check the behavior of key()
  64. $iterator = 1;
  65. foreach($inputs as $key => $input) {
  66. echo "\n-- Iteration $iterator : $key data --\n";
  67. while (key($input) !== NULL) {
  68. var_dump(key($input));
  69. next($input);
  70. }
  71. $iterator++;
  72. };
  73. ?>
  74. --EXPECT--
  75. *** Testing key() : usage variations ***
  76. -- Iteration 1 : int data --
  77. int(0)
  78. int(1)
  79. int(12345)
  80. int(-2345)
  81. -- Iteration 2 : null uppercase data --
  82. string(0) ""
  83. -- Iteration 3 : null lowercase data --
  84. string(0) ""
  85. -- Iteration 4 : bool lowercase data --
  86. int(1)
  87. int(0)
  88. -- Iteration 5 : bool uppercase data --
  89. int(1)
  90. int(0)
  91. -- Iteration 6 : empty double quotes data --
  92. string(0) ""
  93. -- Iteration 7 : empty single quotes data --
  94. string(0) ""
  95. -- Iteration 8 : string data --
  96. string(7) "stringd"
  97. string(7) "strings"
  98. string(11) "hello world"
  99. -- Iteration 9 : undefined data --
  100. string(0) ""
  101. -- Iteration 10 : unset data --
  102. string(0) ""