each_variation3.phpt 4.1 KB

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