each_variation2.phpt 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. array(4) {
  103. [1]=>
  104. int(0)
  105. ["value"]=>
  106. int(0)
  107. [0]=>
  108. int(0)
  109. ["key"]=>
  110. int(0)
  111. }
  112. -- Iteration 2: float data --
  113. array(4) {
  114. [1]=>
  115. float(10.5)
  116. ["value"]=>
  117. float(10.5)
  118. [0]=>
  119. int(0)
  120. ["key"]=>
  121. int(0)
  122. }
  123. -- Iteration 3: null data --
  124. array(4) {
  125. [1]=>
  126. NULL
  127. ["value"]=>
  128. NULL
  129. [0]=>
  130. int(0)
  131. ["key"]=>
  132. int(0)
  133. }
  134. -- Iteration 4: bool data --
  135. array(4) {
  136. [1]=>
  137. bool(true)
  138. ["value"]=>
  139. bool(true)
  140. [0]=>
  141. int(0)
  142. ["key"]=>
  143. int(0)
  144. }
  145. -- Iteration 5: empty string data --
  146. array(4) {
  147. [1]=>
  148. string(0) ""
  149. ["value"]=>
  150. string(0) ""
  151. [0]=>
  152. int(0)
  153. ["key"]=>
  154. int(0)
  155. }
  156. -- Iteration 6: empty array data --
  157. bool(false)
  158. -- Iteration 7: string data --
  159. array(4) {
  160. [1]=>
  161. string(6) "string"
  162. ["value"]=>
  163. string(6) "string"
  164. [0]=>
  165. int(0)
  166. ["key"]=>
  167. int(0)
  168. }
  169. -- Iteration 8: object data --
  170. array(4) {
  171. [1]=>
  172. object(classA)#%d (0) {
  173. }
  174. ["value"]=>
  175. object(classA)#%d (0) {
  176. }
  177. [0]=>
  178. int(0)
  179. ["key"]=>
  180. int(0)
  181. }
  182. -- Iteration 9: undefined data --
  183. array(4) {
  184. [1]=>
  185. NULL
  186. ["value"]=>
  187. NULL
  188. [0]=>
  189. int(0)
  190. ["key"]=>
  191. int(0)
  192. }
  193. -- Iteration 10: unset data --
  194. array(4) {
  195. [1]=>
  196. NULL
  197. ["value"]=>
  198. NULL
  199. [0]=>
  200. int(0)
  201. ["key"]=>
  202. int(0)
  203. }
  204. -- Iteration 11: resource data --
  205. array(4) {
  206. [1]=>
  207. resource(%d) of type (stream)
  208. ["value"]=>
  209. resource(%d) of type (stream)
  210. [0]=>
  211. int(0)
  212. ["key"]=>
  213. int(0)
  214. }
  215. Done