each_variation3.phpt 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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. Deprecated: The each() function is deprecated. This message will be suppressed on further calls in %s on line %d
  91. array(4) {
  92. [1]=>
  93. string(4) "zero"
  94. ["value"]=>
  95. string(4) "zero"
  96. [0]=>
  97. int(0)
  98. ["key"]=>
  99. int(0)
  100. }
  101. -- Iteration 2: float data --
  102. array(4) {
  103. [1]=>
  104. string(8) "positive"
  105. ["value"]=>
  106. string(8) "positive"
  107. [0]=>
  108. int(10)
  109. ["key"]=>
  110. int(10)
  111. }
  112. -- Iteration 3: extreme floats data --
  113. array(4) {
  114. [1]=>
  115. string(5) "large"
  116. ["value"]=>
  117. string(5) "large"
  118. [0]=>
  119. int(12345678)
  120. ["key"]=>
  121. int(12345678)
  122. }
  123. -- Iteration 4: null uppercase data --
  124. array(4) {
  125. [1]=>
  126. string(6) "null 1"
  127. ["value"]=>
  128. string(6) "null 1"
  129. [0]=>
  130. string(0) ""
  131. ["key"]=>
  132. string(0) ""
  133. }
  134. -- Iteration 5: null lowercase data --
  135. array(4) {
  136. [1]=>
  137. string(6) "null 2"
  138. ["value"]=>
  139. string(6) "null 2"
  140. [0]=>
  141. string(0) ""
  142. ["key"]=>
  143. string(0) ""
  144. }
  145. -- Iteration 6: bool lowercase data --
  146. array(4) {
  147. [1]=>
  148. string(6) "lowert"
  149. ["value"]=>
  150. string(6) "lowert"
  151. [0]=>
  152. int(1)
  153. ["key"]=>
  154. int(1)
  155. }
  156. -- Iteration 7: bool uppercase data --
  157. array(4) {
  158. [1]=>
  159. string(6) "uppert"
  160. ["value"]=>
  161. string(6) "uppert"
  162. [0]=>
  163. int(1)
  164. ["key"]=>
  165. int(1)
  166. }
  167. -- Iteration 8: empty double quotes data --
  168. array(4) {
  169. [1]=>
  170. string(6) "emptyd"
  171. ["value"]=>
  172. string(6) "emptyd"
  173. [0]=>
  174. string(0) ""
  175. ["key"]=>
  176. string(0) ""
  177. }
  178. -- Iteration 9: empty single quotes data --
  179. array(4) {
  180. [1]=>
  181. string(6) "emptys"
  182. ["value"]=>
  183. string(6) "emptys"
  184. [0]=>
  185. string(0) ""
  186. ["key"]=>
  187. string(0) ""
  188. }
  189. -- Iteration 10: string data --
  190. array(4) {
  191. [1]=>
  192. string(7) "stringd"
  193. ["value"]=>
  194. string(7) "stringd"
  195. [0]=>
  196. string(7) "stringd"
  197. ["key"]=>
  198. string(7) "stringd"
  199. }
  200. -- Iteration 11: undefined data --
  201. array(4) {
  202. [1]=>
  203. string(9) "undefined"
  204. ["value"]=>
  205. string(9) "undefined"
  206. [0]=>
  207. string(0) ""
  208. ["key"]=>
  209. string(0) ""
  210. }
  211. -- Iteration 12: unset data --
  212. array(4) {
  213. [1]=>
  214. string(5) "unset"
  215. ["value"]=>
  216. string(5) "unset"
  217. [0]=>
  218. string(0) ""
  219. ["key"]=>
  220. string(0) ""
  221. }
  222. Done