each_variation2.phpt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. --TEST--
  2. Test each() function : usage variations - arrays 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 arrays of different data types as $arr argument to each() 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. // get a class
  18. class classA
  19. {
  20. public function __toString() {
  21. return "Class A object";
  22. }
  23. }
  24. // heredoc string
  25. $heredoc = <<<EOT
  26. hello world
  27. EOT;
  28. // get a resource variable
  29. $fp = fopen(__FILE__, "r");
  30. // arrays of different data types to be passed as $arr
  31. $inputs = array(
  32. // int data
  33. /*1*/ 'int' => array(
  34. 0,
  35. 1,
  36. 12345,
  37. -2345,
  38. ),
  39. // float data
  40. /*2*/ 'float' => array(
  41. 10.5,
  42. -10.5,
  43. 12.3456789000e10,
  44. 12.3456789000E-10,
  45. .5,
  46. ),
  47. // null data
  48. /*3*/ 'null' => array(
  49. NULL,
  50. null,
  51. ),
  52. // boolean data
  53. /*4*/ 'bool' => array(
  54. true,
  55. false,
  56. TRUE,
  57. FALSE,
  58. ),
  59. // empty data
  60. /*5*/ 'empty string' => array(
  61. "",
  62. '',
  63. ),
  64. /*6*/ 'empty array' => array(
  65. ),
  66. // string data
  67. /*7*/ 'string' => array(
  68. "string",
  69. 'string',
  70. $heredoc,
  71. ),
  72. // object data
  73. /*8*/ 'object' => array(
  74. new classA(),
  75. ),
  76. // undefined data
  77. /*9*/ 'undefined' => array(
  78. @$undefined_var,
  79. ),
  80. // unset data
  81. /*10*/ 'unset' => array(
  82. @$unset_var,
  83. ),
  84. // resource variable
  85. /*11*/ 'resource' => array(
  86. $fp
  87. ),
  88. );
  89. // loop through each element of $inputs to check the behavior of each()
  90. $iterator = 1;
  91. foreach($inputs as $key => $input) {
  92. echo "\n-- Iteration $iterator: $key data --\n";
  93. var_dump( each($input) );
  94. $iterator++;
  95. };
  96. fclose($fp);
  97. echo "Done";
  98. ?>
  99. --EXPECTF--
  100. *** Testing each() : usage variations ***
  101. -- Iteration 1: int data --
  102. Deprecated: The each() function is deprecated. This message will be suppressed on further calls in %s on line %d
  103. array(4) {
  104. [1]=>
  105. int(0)
  106. ["value"]=>
  107. int(0)
  108. [0]=>
  109. int(0)
  110. ["key"]=>
  111. int(0)
  112. }
  113. -- Iteration 2: float data --
  114. array(4) {
  115. [1]=>
  116. float(10.5)
  117. ["value"]=>
  118. float(10.5)
  119. [0]=>
  120. int(0)
  121. ["key"]=>
  122. int(0)
  123. }
  124. -- Iteration 3: null data --
  125. array(4) {
  126. [1]=>
  127. NULL
  128. ["value"]=>
  129. NULL
  130. [0]=>
  131. int(0)
  132. ["key"]=>
  133. int(0)
  134. }
  135. -- Iteration 4: bool data --
  136. array(4) {
  137. [1]=>
  138. bool(true)
  139. ["value"]=>
  140. bool(true)
  141. [0]=>
  142. int(0)
  143. ["key"]=>
  144. int(0)
  145. }
  146. -- Iteration 5: empty string data --
  147. array(4) {
  148. [1]=>
  149. string(0) ""
  150. ["value"]=>
  151. string(0) ""
  152. [0]=>
  153. int(0)
  154. ["key"]=>
  155. int(0)
  156. }
  157. -- Iteration 6: empty array data --
  158. bool(false)
  159. -- Iteration 7: string data --
  160. array(4) {
  161. [1]=>
  162. string(6) "string"
  163. ["value"]=>
  164. string(6) "string"
  165. [0]=>
  166. int(0)
  167. ["key"]=>
  168. int(0)
  169. }
  170. -- Iteration 8: object data --
  171. array(4) {
  172. [1]=>
  173. object(classA)#%d (0) {
  174. }
  175. ["value"]=>
  176. object(classA)#%d (0) {
  177. }
  178. [0]=>
  179. int(0)
  180. ["key"]=>
  181. int(0)
  182. }
  183. -- Iteration 9: undefined data --
  184. array(4) {
  185. [1]=>
  186. NULL
  187. ["value"]=>
  188. NULL
  189. [0]=>
  190. int(0)
  191. ["key"]=>
  192. int(0)
  193. }
  194. -- Iteration 10: unset data --
  195. array(4) {
  196. [1]=>
  197. NULL
  198. ["value"]=>
  199. NULL
  200. [0]=>
  201. int(0)
  202. ["key"]=>
  203. int(0)
  204. }
  205. -- Iteration 11: resource data --
  206. array(4) {
  207. [1]=>
  208. resource(%d) of type (stream)
  209. ["value"]=>
  210. resource(%d) of type (stream)
  211. [0]=>
  212. int(0)
  213. ["key"]=>
  214. int(0)
  215. }
  216. Done