key_variation2.phpt 2.9 KB

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