1--TEST--
2Test 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/*
11 * Pass different data types as $preserve_keys argument to array_slice() to test behaviour
12 */
13
14echo "*** Testing array_slice() : usage variations ***\n";
15
16// Initialise function arguments not being substituted
17$input_array = array('one' => 1, 2, 99 => 3, 4);
18$offset = 0;
19$length = 3;
20
21//get an unset variable
22$unset_var = 10;
23unset ($unset_var);
24
25// get a class
26class classA
27{
28  public function __toString() {
29    return "Class A object";
30  }
31}
32
33// heredoc string
34$heredoc = <<<EOT
35hello world
36EOT;
37
38// unexpected values to be passed to $preserve_keys argument
39$inputs = array(
40
41       // int data
42/*1*/  0,
43       1,
44       12345,
45       -2345,
46
47       // float data
48/*5*/  10.5,
49       -10.5,
50       12.3456789000e10,
51       12.3456789000E-10,
52       .5,
53
54       // null data
55/*10*/ NULL,
56       null,
57
58       // boolean data
59/*12*/ true,
60       false,
61       TRUE,
62       FALSE,
63
64       // empty data
65/*16*/ "",
66       '',
67       array(),
68
69       // string data
70/*19*/ "string",
71       'string',
72       $heredoc,
73
74       // object data
75/*22*/ new classA(),
76
77       // undefined data
78/*23*/ @$undefined_var,
79
80       // unset data
81/*24*/ @$unset_var,
82);
83
84// loop through each element of $inputs to check the behavior of array_slice()
85$iterator = 1;
86foreach($inputs as $input) {
87  echo "\n-- Iteration $iterator --\n";
88  var_dump( array_slice($input_array, $offset, $length, $input) );
89  $iterator++;
90};
91
92echo "Done";
93?>
94
95--EXPECTF--
96*** Testing array_slice() : usage variations ***
97
98-- Iteration 1 --
99array(3) {
100  ["one"]=>
101  int(1)
102  [0]=>
103  int(2)
104  [1]=>
105  int(3)
106}
107
108-- Iteration 2 --
109array(3) {
110  ["one"]=>
111  int(1)
112  [0]=>
113  int(2)
114  [99]=>
115  int(3)
116}
117
118-- Iteration 3 --
119array(3) {
120  ["one"]=>
121  int(1)
122  [0]=>
123  int(2)
124  [99]=>
125  int(3)
126}
127
128-- Iteration 4 --
129array(3) {
130  ["one"]=>
131  int(1)
132  [0]=>
133  int(2)
134  [99]=>
135  int(3)
136}
137
138-- Iteration 5 --
139array(3) {
140  ["one"]=>
141  int(1)
142  [0]=>
143  int(2)
144  [99]=>
145  int(3)
146}
147
148-- Iteration 6 --
149array(3) {
150  ["one"]=>
151  int(1)
152  [0]=>
153  int(2)
154  [99]=>
155  int(3)
156}
157
158-- Iteration 7 --
159array(3) {
160  ["one"]=>
161  int(1)
162  [0]=>
163  int(2)
164  [99]=>
165  int(3)
166}
167
168-- Iteration 8 --
169array(3) {
170  ["one"]=>
171  int(1)
172  [0]=>
173  int(2)
174  [99]=>
175  int(3)
176}
177
178-- Iteration 9 --
179array(3) {
180  ["one"]=>
181  int(1)
182  [0]=>
183  int(2)
184  [99]=>
185  int(3)
186}
187
188-- Iteration 10 --
189array(3) {
190  ["one"]=>
191  int(1)
192  [0]=>
193  int(2)
194  [1]=>
195  int(3)
196}
197
198-- Iteration 11 --
199array(3) {
200  ["one"]=>
201  int(1)
202  [0]=>
203  int(2)
204  [1]=>
205  int(3)
206}
207
208-- Iteration 12 --
209array(3) {
210  ["one"]=>
211  int(1)
212  [0]=>
213  int(2)
214  [99]=>
215  int(3)
216}
217
218-- Iteration 13 --
219array(3) {
220  ["one"]=>
221  int(1)
222  [0]=>
223  int(2)
224  [1]=>
225  int(3)
226}
227
228-- Iteration 14 --
229array(3) {
230  ["one"]=>
231  int(1)
232  [0]=>
233  int(2)
234  [99]=>
235  int(3)
236}
237
238-- Iteration 15 --
239array(3) {
240  ["one"]=>
241  int(1)
242  [0]=>
243  int(2)
244  [1]=>
245  int(3)
246}
247
248-- Iteration 16 --
249array(3) {
250  ["one"]=>
251  int(1)
252  [0]=>
253  int(2)
254  [1]=>
255  int(3)
256}
257
258-- Iteration 17 --
259array(3) {
260  ["one"]=>
261  int(1)
262  [0]=>
263  int(2)
264  [1]=>
265  int(3)
266}
267
268-- Iteration 18 --
269
270Warning: array_slice() expects parameter 4 to be boolean, array given in %s on line %d
271NULL
272
273-- Iteration 19 --
274array(3) {
275  ["one"]=>
276  int(1)
277  [0]=>
278  int(2)
279  [99]=>
280  int(3)
281}
282
283-- Iteration 20 --
284array(3) {
285  ["one"]=>
286  int(1)
287  [0]=>
288  int(2)
289  [99]=>
290  int(3)
291}
292
293-- Iteration 21 --
294array(3) {
295  ["one"]=>
296  int(1)
297  [0]=>
298  int(2)
299  [99]=>
300  int(3)
301}
302
303-- Iteration 22 --
304
305Warning: array_slice() expects parameter 4 to be boolean, object given in %s on line %d
306NULL
307
308-- Iteration 23 --
309array(3) {
310  ["one"]=>
311  int(1)
312  [0]=>
313  int(2)
314  [1]=>
315  int(3)
316}
317
318-- Iteration 24 --
319array(3) {
320  ["one"]=>
321  int(1)
322  [0]=>
323  int(2)
324  [1]=>
325  int(3)
326}
327Done