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