array_slice_variation2.phpt 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  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. array(4) {
  129. ["one"]=>
  130. int(1)
  131. [0]=>
  132. int(2)
  133. ["three"]=>
  134. int(3)
  135. [1]=>
  136. int(4)
  137. }
  138. -- Iteration 8 --
  139. array(4) {
  140. ["one"]=>
  141. int(1)
  142. [0]=>
  143. int(2)
  144. ["three"]=>
  145. int(3)
  146. [1]=>
  147. int(4)
  148. }
  149. -- Iteration 9 --
  150. array(4) {
  151. ["one"]=>
  152. int(1)
  153. [0]=>
  154. int(2)
  155. ["three"]=>
  156. int(3)
  157. [1]=>
  158. int(4)
  159. }
  160. -- Iteration 10 --
  161. array(4) {
  162. ["one"]=>
  163. int(1)
  164. [0]=>
  165. int(2)
  166. ["three"]=>
  167. int(3)
  168. [1]=>
  169. int(4)
  170. }
  171. -- Iteration 11 --
  172. array(4) {
  173. ["one"]=>
  174. int(1)
  175. [0]=>
  176. int(2)
  177. ["three"]=>
  178. int(3)
  179. [1]=>
  180. int(4)
  181. }
  182. -- Iteration 12 --
  183. array(3) {
  184. [0]=>
  185. int(2)
  186. ["three"]=>
  187. int(3)
  188. [1]=>
  189. int(4)
  190. }
  191. -- Iteration 13 --
  192. array(4) {
  193. ["one"]=>
  194. int(1)
  195. [0]=>
  196. int(2)
  197. ["three"]=>
  198. int(3)
  199. [1]=>
  200. int(4)
  201. }
  202. -- Iteration 14 --
  203. array(3) {
  204. [0]=>
  205. int(2)
  206. ["three"]=>
  207. int(3)
  208. [1]=>
  209. int(4)
  210. }
  211. -- Iteration 15 --
  212. array(4) {
  213. ["one"]=>
  214. int(1)
  215. [0]=>
  216. int(2)
  217. ["three"]=>
  218. int(3)
  219. [1]=>
  220. int(4)
  221. }
  222. -- Iteration 16 --
  223. Warning: array_slice() expects parameter 2 to be long, string given in %s on line %d
  224. NULL
  225. -- Iteration 17 --
  226. Warning: array_slice() expects parameter 2 to be long, string given in %s on line %d
  227. NULL
  228. -- Iteration 18 --
  229. Warning: array_slice() expects parameter 2 to be long, array given in %s on line %d
  230. NULL
  231. -- Iteration 19 --
  232. Warning: array_slice() expects parameter 2 to be long, string given in %s on line %d
  233. NULL
  234. -- Iteration 20 --
  235. Warning: array_slice() expects parameter 2 to be long, string given in %s on line %d
  236. NULL
  237. -- Iteration 21 --
  238. Warning: array_slice() expects parameter 2 to be long, string given in %s on line %d
  239. NULL
  240. -- Iteration 22 --
  241. array(4) {
  242. ["one"]=>
  243. int(1)
  244. [0]=>
  245. int(2)
  246. ["three"]=>
  247. int(3)
  248. [1]=>
  249. int(4)
  250. }
  251. -- Iteration 23 --
  252. array(4) {
  253. ["one"]=>
  254. int(1)
  255. [0]=>
  256. int(2)
  257. ["three"]=>
  258. int(3)
  259. [1]=>
  260. int(4)
  261. }
  262. Done