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 -- 155 156Warning: array_slice() expects parameter 2 to be integer, float given in %s on line %d 157NULL 158 159-- Iteration 8 -- 160array(4) { 161 ["one"]=> 162 int(1) 163 [0]=> 164 int(2) 165 ["three"]=> 166 int(3) 167 [1]=> 168 int(4) 169} 170 171-- Iteration 9 -- 172array(4) { 173 ["one"]=> 174 int(1) 175 [0]=> 176 int(2) 177 ["three"]=> 178 int(3) 179 [1]=> 180 int(4) 181} 182 183-- Iteration 10 -- 184array(4) { 185 ["one"]=> 186 int(1) 187 [0]=> 188 int(2) 189 ["three"]=> 190 int(3) 191 [1]=> 192 int(4) 193} 194 195-- Iteration 11 -- 196array(4) { 197 ["one"]=> 198 int(1) 199 [0]=> 200 int(2) 201 ["three"]=> 202 int(3) 203 [1]=> 204 int(4) 205} 206 207-- Iteration 12 -- 208array(3) { 209 [0]=> 210 int(2) 211 ["three"]=> 212 int(3) 213 [1]=> 214 int(4) 215} 216 217-- Iteration 13 -- 218array(4) { 219 ["one"]=> 220 int(1) 221 [0]=> 222 int(2) 223 ["three"]=> 224 int(3) 225 [1]=> 226 int(4) 227} 228 229-- Iteration 14 -- 230array(3) { 231 [0]=> 232 int(2) 233 ["three"]=> 234 int(3) 235 [1]=> 236 int(4) 237} 238 239-- Iteration 15 -- 240array(4) { 241 ["one"]=> 242 int(1) 243 [0]=> 244 int(2) 245 ["three"]=> 246 int(3) 247 [1]=> 248 int(4) 249} 250 251-- Iteration 16 -- 252 253Warning: array_slice() expects parameter 2 to be integer, string given in %s on line %d 254NULL 255 256-- Iteration 17 -- 257 258Warning: array_slice() expects parameter 2 to be integer, string given in %s on line %d 259NULL 260 261-- Iteration 18 -- 262 263Warning: array_slice() expects parameter 2 to be integer, array given in %s on line %d 264NULL 265 266-- Iteration 19 -- 267 268Warning: array_slice() expects parameter 2 to be integer, string given in %s on line %d 269NULL 270 271-- Iteration 20 -- 272 273Warning: array_slice() expects parameter 2 to be integer, string given in %s on line %d 274NULL 275 276-- Iteration 21 -- 277 278Warning: array_slice() expects parameter 2 to be integer, string given in %s on line %d 279NULL 280 281-- Iteration 22 -- 282array(4) { 283 ["one"]=> 284 int(1) 285 [0]=> 286 int(2) 287 ["three"]=> 288 int(3) 289 [1]=> 290 int(4) 291} 292 293-- Iteration 23 -- 294array(4) { 295 ["one"]=> 296 int(1) 297 [0]=> 298 int(2) 299 ["three"]=> 300 int(3) 301 [1]=> 302 int(4) 303} 304Done