array_slice_variation4.phpt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. --TEST--
  2. Test array_slice() function : usage variations - Pass different data types as $preserve_keys arg
  3. --FILE--
  4. <?php
  5. /* Prototype : array array_slice(array $input, int $offset [, int $length [, bool $preserve_keys]])
  6. * Description: Returns elements specified by offset and length
  7. * Source code: ext/standard/array.c
  8. */
  9. /*
  10. * Pass different data types as $preserve_keys argument to array_slice() to test behaviour
  11. */
  12. echo "*** Testing array_slice() : usage variations ***\n";
  13. // Initialise function arguments not being substituted
  14. $input_array = array('one' => 1, 2, 99 => 3, 4);
  15. $offset = 0;
  16. $length = 3;
  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. // unexpected values to be passed to $preserve_keys argument
  32. $inputs = array(
  33. // int data
  34. /*1*/ 0,
  35. 1,
  36. 12345,
  37. -2345,
  38. // float data
  39. /*5*/ 10.5,
  40. -10.5,
  41. 12.3456789000e10,
  42. 12.3456789000E-10,
  43. .5,
  44. // null data
  45. /*10*/ NULL,
  46. null,
  47. // boolean data
  48. /*12*/ true,
  49. false,
  50. TRUE,
  51. FALSE,
  52. // empty data
  53. /*16*/ "",
  54. '',
  55. array(),
  56. // string data
  57. /*19*/ "string",
  58. 'string',
  59. $heredoc,
  60. // object data
  61. /*22*/ new classA(),
  62. // undefined data
  63. /*23*/ @$undefined_var,
  64. // unset data
  65. /*24*/ @$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, $offset, $length, $input) );
  72. $iterator++;
  73. };
  74. echo "Done";
  75. ?>
  76. --EXPECTF--
  77. *** Testing array_slice() : usage variations ***
  78. -- Iteration 1 --
  79. array(3) {
  80. ["one"]=>
  81. int(1)
  82. [0]=>
  83. int(2)
  84. [1]=>
  85. int(3)
  86. }
  87. -- Iteration 2 --
  88. array(3) {
  89. ["one"]=>
  90. int(1)
  91. [0]=>
  92. int(2)
  93. [99]=>
  94. int(3)
  95. }
  96. -- Iteration 3 --
  97. array(3) {
  98. ["one"]=>
  99. int(1)
  100. [0]=>
  101. int(2)
  102. [99]=>
  103. int(3)
  104. }
  105. -- Iteration 4 --
  106. array(3) {
  107. ["one"]=>
  108. int(1)
  109. [0]=>
  110. int(2)
  111. [99]=>
  112. int(3)
  113. }
  114. -- Iteration 5 --
  115. array(3) {
  116. ["one"]=>
  117. int(1)
  118. [0]=>
  119. int(2)
  120. [99]=>
  121. int(3)
  122. }
  123. -- Iteration 6 --
  124. array(3) {
  125. ["one"]=>
  126. int(1)
  127. [0]=>
  128. int(2)
  129. [99]=>
  130. int(3)
  131. }
  132. -- Iteration 7 --
  133. array(3) {
  134. ["one"]=>
  135. int(1)
  136. [0]=>
  137. int(2)
  138. [99]=>
  139. int(3)
  140. }
  141. -- Iteration 8 --
  142. array(3) {
  143. ["one"]=>
  144. int(1)
  145. [0]=>
  146. int(2)
  147. [99]=>
  148. int(3)
  149. }
  150. -- Iteration 9 --
  151. array(3) {
  152. ["one"]=>
  153. int(1)
  154. [0]=>
  155. int(2)
  156. [99]=>
  157. int(3)
  158. }
  159. -- Iteration 10 --
  160. array(3) {
  161. ["one"]=>
  162. int(1)
  163. [0]=>
  164. int(2)
  165. [1]=>
  166. int(3)
  167. }
  168. -- Iteration 11 --
  169. array(3) {
  170. ["one"]=>
  171. int(1)
  172. [0]=>
  173. int(2)
  174. [1]=>
  175. int(3)
  176. }
  177. -- Iteration 12 --
  178. array(3) {
  179. ["one"]=>
  180. int(1)
  181. [0]=>
  182. int(2)
  183. [99]=>
  184. int(3)
  185. }
  186. -- Iteration 13 --
  187. array(3) {
  188. ["one"]=>
  189. int(1)
  190. [0]=>
  191. int(2)
  192. [1]=>
  193. int(3)
  194. }
  195. -- Iteration 14 --
  196. array(3) {
  197. ["one"]=>
  198. int(1)
  199. [0]=>
  200. int(2)
  201. [99]=>
  202. int(3)
  203. }
  204. -- Iteration 15 --
  205. array(3) {
  206. ["one"]=>
  207. int(1)
  208. [0]=>
  209. int(2)
  210. [1]=>
  211. int(3)
  212. }
  213. -- Iteration 16 --
  214. array(3) {
  215. ["one"]=>
  216. int(1)
  217. [0]=>
  218. int(2)
  219. [1]=>
  220. int(3)
  221. }
  222. -- Iteration 17 --
  223. array(3) {
  224. ["one"]=>
  225. int(1)
  226. [0]=>
  227. int(2)
  228. [1]=>
  229. int(3)
  230. }
  231. -- Iteration 18 --
  232. Warning: array_slice() expects parameter 4 to be boolean, array given in %s on line %d
  233. NULL
  234. -- Iteration 19 --
  235. array(3) {
  236. ["one"]=>
  237. int(1)
  238. [0]=>
  239. int(2)
  240. [99]=>
  241. int(3)
  242. }
  243. -- Iteration 20 --
  244. array(3) {
  245. ["one"]=>
  246. int(1)
  247. [0]=>
  248. int(2)
  249. [99]=>
  250. int(3)
  251. }
  252. -- Iteration 21 --
  253. array(3) {
  254. ["one"]=>
  255. int(1)
  256. [0]=>
  257. int(2)
  258. [99]=>
  259. int(3)
  260. }
  261. -- Iteration 22 --
  262. Warning: array_slice() expects parameter 4 to be boolean, object given in %s on line %d
  263. NULL
  264. -- Iteration 23 --
  265. array(3) {
  266. ["one"]=>
  267. int(1)
  268. [0]=>
  269. int(2)
  270. [1]=>
  271. int(3)
  272. }
  273. -- Iteration 24 --
  274. array(3) {
  275. ["one"]=>
  276. int(1)
  277. [0]=>
  278. int(2)
  279. [1]=>
  280. int(3)
  281. }
  282. Done