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--EXPECTF-- 95*** Testing array_slice() : usage variations *** 96 97-- Iteration 1 -- 98array(3) { 99 ["one"]=> 100 int(1) 101 [0]=> 102 int(2) 103 [1]=> 104 int(3) 105} 106 107-- Iteration 2 -- 108array(3) { 109 ["one"]=> 110 int(1) 111 [0]=> 112 int(2) 113 [99]=> 114 int(3) 115} 116 117-- Iteration 3 -- 118array(3) { 119 ["one"]=> 120 int(1) 121 [0]=> 122 int(2) 123 [99]=> 124 int(3) 125} 126 127-- Iteration 4 -- 128array(3) { 129 ["one"]=> 130 int(1) 131 [0]=> 132 int(2) 133 [99]=> 134 int(3) 135} 136 137-- Iteration 5 -- 138array(3) { 139 ["one"]=> 140 int(1) 141 [0]=> 142 int(2) 143 [99]=> 144 int(3) 145} 146 147-- Iteration 6 -- 148array(3) { 149 ["one"]=> 150 int(1) 151 [0]=> 152 int(2) 153 [99]=> 154 int(3) 155} 156 157-- Iteration 7 -- 158array(3) { 159 ["one"]=> 160 int(1) 161 [0]=> 162 int(2) 163 [99]=> 164 int(3) 165} 166 167-- Iteration 8 -- 168array(3) { 169 ["one"]=> 170 int(1) 171 [0]=> 172 int(2) 173 [99]=> 174 int(3) 175} 176 177-- Iteration 9 -- 178array(3) { 179 ["one"]=> 180 int(1) 181 [0]=> 182 int(2) 183 [99]=> 184 int(3) 185} 186 187-- Iteration 10 -- 188array(3) { 189 ["one"]=> 190 int(1) 191 [0]=> 192 int(2) 193 [1]=> 194 int(3) 195} 196 197-- Iteration 11 -- 198array(3) { 199 ["one"]=> 200 int(1) 201 [0]=> 202 int(2) 203 [1]=> 204 int(3) 205} 206 207-- Iteration 12 -- 208array(3) { 209 ["one"]=> 210 int(1) 211 [0]=> 212 int(2) 213 [99]=> 214 int(3) 215} 216 217-- Iteration 13 -- 218array(3) { 219 ["one"]=> 220 int(1) 221 [0]=> 222 int(2) 223 [1]=> 224 int(3) 225} 226 227-- Iteration 14 -- 228array(3) { 229 ["one"]=> 230 int(1) 231 [0]=> 232 int(2) 233 [99]=> 234 int(3) 235} 236 237-- Iteration 15 -- 238array(3) { 239 ["one"]=> 240 int(1) 241 [0]=> 242 int(2) 243 [1]=> 244 int(3) 245} 246 247-- Iteration 16 -- 248array(3) { 249 ["one"]=> 250 int(1) 251 [0]=> 252 int(2) 253 [1]=> 254 int(3) 255} 256 257-- Iteration 17 -- 258array(3) { 259 ["one"]=> 260 int(1) 261 [0]=> 262 int(2) 263 [1]=> 264 int(3) 265} 266 267-- Iteration 18 -- 268 269Warning: array_slice() expects parameter 4 to be bool, array given in %s on line %d 270NULL 271 272-- Iteration 19 -- 273array(3) { 274 ["one"]=> 275 int(1) 276 [0]=> 277 int(2) 278 [99]=> 279 int(3) 280} 281 282-- Iteration 20 -- 283array(3) { 284 ["one"]=> 285 int(1) 286 [0]=> 287 int(2) 288 [99]=> 289 int(3) 290} 291 292-- Iteration 21 -- 293array(3) { 294 ["one"]=> 295 int(1) 296 [0]=> 297 int(2) 298 [99]=> 299 int(3) 300} 301 302-- Iteration 22 -- 303 304Warning: array_slice() expects parameter 4 to be bool, object given in %s on line %d 305NULL 306 307-- Iteration 23 -- 308array(3) { 309 ["one"]=> 310 int(1) 311 [0]=> 312 int(2) 313 [1]=> 314 int(3) 315} 316 317-- Iteration 24 -- 318array(3) { 319 ["one"]=> 320 int(1) 321 [0]=> 322 int(2) 323 [1]=> 324 int(3) 325} 326Done 327