array_slice_variation2.phpt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. --TEST--
  2. Test array_slice() function : usage variations - Pass different data types as $offset arg
  3. --SKIPIF--
  4. <?php if (PHP_INT_SIZE > 4) die("skip this test is for 32bit platform only"); ?>
  5. --FILE--
  6. <?php
  7. /* Prototype : array array_slice(array $input, int $offset [, int $length [, bool $preserve_keys]])
  8. * Description: Returns elements specified by offset and length
  9. * Source code: ext/standard/array.c
  10. */
  11. /*
  12. * Pass different data types as $offset argument to array_slice() to test behaviour
  13. */
  14. echo "*** Testing array_slice() : usage variations ***\n";
  15. // Initialise function arguments not being substituted
  16. $input_array = array('one' => 1, 2, 'three' => 3, 4);
  17. //get an unset variable
  18. $unset_var = 10;
  19. unset ($unset_var);
  20. // get a class
  21. class classA
  22. {
  23. public function __toString() {
  24. return "Class A object";
  25. }
  26. }
  27. // heredoc string
  28. $heredoc = <<<EOT
  29. hello world
  30. EOT;
  31. // get a resource variable
  32. $fp = fopen(__FILE__, "r");
  33. // unexpected values to be passed to $offset argument
  34. $inputs = array(
  35. // int data
  36. /*1*/ 0,
  37. 1,
  38. 12345,
  39. -2345,
  40. // float data
  41. /*5*/ 10.5,
  42. -10.5,
  43. 12.3456789000e10,
  44. 12.3456789000E-10,
  45. .5,
  46. // null data
  47. /*10*/ NULL,
  48. null,
  49. // boolean data
  50. /*12*/ true,
  51. false,
  52. TRUE,
  53. FALSE,
  54. // empty data
  55. /*16*/ "",
  56. '',
  57. array(),
  58. // string data
  59. /*19*/ "string",
  60. 'string',
  61. $heredoc,
  62. // undefined data
  63. /*22*/ @$undefined_var,
  64. // unset data
  65. /*23*/ @$unset_var,
  66. );
  67. // loop through each element of $inputs to check the behavior of array_slice()
  68. $iterator = 1;
  69. foreach($inputs as $input) {
  70. echo "\n-- Iteration $iterator --\n";
  71. var_dump( array_slice($input_array, $input) );
  72. $iterator++;
  73. };
  74. fclose($fp);
  75. echo "Done";
  76. ?>
  77. --EXPECTF--
  78. *** Testing array_slice() : usage variations ***
  79. -- Iteration 1 --
  80. array(4) {
  81. ["one"]=>
  82. int(1)
  83. [0]=>
  84. int(2)
  85. ["three"]=>
  86. int(3)
  87. [1]=>
  88. int(4)
  89. }
  90. -- Iteration 2 --
  91. array(3) {
  92. [0]=>
  93. int(2)
  94. ["three"]=>
  95. int(3)
  96. [1]=>
  97. int(4)
  98. }
  99. -- Iteration 3 --
  100. array(0) {
  101. }
  102. -- Iteration 4 --
  103. array(4) {
  104. ["one"]=>
  105. int(1)
  106. [0]=>
  107. int(2)
  108. ["three"]=>
  109. int(3)
  110. [1]=>
  111. int(4)
  112. }
  113. -- Iteration 5 --
  114. array(0) {
  115. }
  116. -- Iteration 6 --
  117. array(4) {
  118. ["one"]=>
  119. int(1)
  120. [0]=>
  121. int(2)
  122. ["three"]=>
  123. int(3)
  124. [1]=>
  125. int(4)
  126. }
  127. -- Iteration 7 --
  128. Warning: array_slice() expects parameter 2 to be int, float given in %s on line %d
  129. NULL
  130. -- Iteration 8 --
  131. array(4) {
  132. ["one"]=>
  133. int(1)
  134. [0]=>
  135. int(2)
  136. ["three"]=>
  137. int(3)
  138. [1]=>
  139. int(4)
  140. }
  141. -- Iteration 9 --
  142. array(4) {
  143. ["one"]=>
  144. int(1)
  145. [0]=>
  146. int(2)
  147. ["three"]=>
  148. int(3)
  149. [1]=>
  150. int(4)
  151. }
  152. -- Iteration 10 --
  153. array(4) {
  154. ["one"]=>
  155. int(1)
  156. [0]=>
  157. int(2)
  158. ["three"]=>
  159. int(3)
  160. [1]=>
  161. int(4)
  162. }
  163. -- Iteration 11 --
  164. array(4) {
  165. ["one"]=>
  166. int(1)
  167. [0]=>
  168. int(2)
  169. ["three"]=>
  170. int(3)
  171. [1]=>
  172. int(4)
  173. }
  174. -- Iteration 12 --
  175. array(3) {
  176. [0]=>
  177. int(2)
  178. ["three"]=>
  179. int(3)
  180. [1]=>
  181. int(4)
  182. }
  183. -- Iteration 13 --
  184. array(4) {
  185. ["one"]=>
  186. int(1)
  187. [0]=>
  188. int(2)
  189. ["three"]=>
  190. int(3)
  191. [1]=>
  192. int(4)
  193. }
  194. -- Iteration 14 --
  195. array(3) {
  196. [0]=>
  197. int(2)
  198. ["three"]=>
  199. int(3)
  200. [1]=>
  201. int(4)
  202. }
  203. -- Iteration 15 --
  204. array(4) {
  205. ["one"]=>
  206. int(1)
  207. [0]=>
  208. int(2)
  209. ["three"]=>
  210. int(3)
  211. [1]=>
  212. int(4)
  213. }
  214. -- Iteration 16 --
  215. Warning: array_slice() expects parameter 2 to be int, string given in %s on line %d
  216. NULL
  217. -- Iteration 17 --
  218. Warning: array_slice() expects parameter 2 to be int, string given in %s on line %d
  219. NULL
  220. -- Iteration 18 --
  221. Warning: array_slice() expects parameter 2 to be int, array given in %s on line %d
  222. NULL
  223. -- Iteration 19 --
  224. Warning: array_slice() expects parameter 2 to be int, string given in %s on line %d
  225. NULL
  226. -- Iteration 20 --
  227. Warning: array_slice() expects parameter 2 to be int, string given in %s on line %d
  228. NULL
  229. -- Iteration 21 --
  230. Warning: array_slice() expects parameter 2 to be int, string given in %s on line %d
  231. NULL
  232. -- Iteration 22 --
  233. array(4) {
  234. ["one"]=>
  235. int(1)
  236. [0]=>
  237. int(2)
  238. ["three"]=>
  239. int(3)
  240. [1]=>
  241. int(4)
  242. }
  243. -- Iteration 23 --
  244. array(4) {
  245. ["one"]=>
  246. int(1)
  247. [0]=>
  248. int(2)
  249. ["three"]=>
  250. int(3)
  251. [1]=>
  252. int(4)
  253. }
  254. Done