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