1--TEST-- 2Test explode() function : usage variations - test values for $limit argument 3--FILE-- 4<?php 5 6/* Prototype : array explode ( string $delimiter , string $string [, int $limit ] ) 7 * Description: Split a string by string. 8 * Source code: ext/standard/string.c 9*/ 10 11echo "*** Testing explode() function: with unexpected inputs for 'limit' argument ***\n"; 12 13//get an unset variable 14$unset_var = 'string_val'; 15unset($unset_var); 16 17//defining a class 18class sample { 19 public function __toString() { 20 return "sample object"; 21 } 22} 23 24//getting the resource 25$file_handle = fopen(__FILE__, "r"); 26 27// array with different values for $delimiter 28$limits = array ( 29 30 // integer values 31/*1*/ 0, 32 1, 33 255, 34 256, 35 2147483647, 36 -2147483648, 37 38 // float values 39/*7*/ 10.5, 40 -20.5, 41 10.1234567e5, 42 43 // array values 44/*10*/ array(), 45 array(0), 46 array(1, 2), 47 48 // boolean values 49/*13*/ true, 50 false, 51 TRUE, 52 FALSE, 53 54 // null values 55/*17*/ NULL, 56 null, 57 58 // objects 59/*19*/ new sample(), 60 61 // resource 62/*20*/ $file_handle, 63 64 // undefined variable 65/*21*/ @$undefined_var, 66 67 // unset variable 68/*22*/ @$unset_var 69); 70 71// loop through with each element of the $limits array to test explode() function 72$count = 1; 73$delimiter = " "; 74$string = "piece1 piece2 piece3 piece4 piece5 piece6"; 75foreach($limits as $limit) { 76 echo "-- Iteration $count --\n"; 77 var_dump( explode($delimiter, $string, $limit) ); 78 $count ++; 79} 80 81fclose($file_handle); //closing the file handle 82 83?> 84===Done=== 85--EXPECTF-- 86*** Testing explode() function: with unexpected inputs for 'limit' argument *** 87-- Iteration 1 -- 88array(1) { 89 [0]=> 90 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 91} 92-- Iteration 2 -- 93array(1) { 94 [0]=> 95 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 96} 97-- Iteration 3 -- 98array(6) { 99 [0]=> 100 string(6) "piece1" 101 [1]=> 102 string(6) "piece2" 103 [2]=> 104 string(6) "piece3" 105 [3]=> 106 string(6) "piece4" 107 [4]=> 108 string(6) "piece5" 109 [5]=> 110 string(6) "piece6" 111} 112-- Iteration 4 -- 113array(6) { 114 [0]=> 115 string(6) "piece1" 116 [1]=> 117 string(6) "piece2" 118 [2]=> 119 string(6) "piece3" 120 [3]=> 121 string(6) "piece4" 122 [4]=> 123 string(6) "piece5" 124 [5]=> 125 string(6) "piece6" 126} 127-- Iteration 5 -- 128array(6) { 129 [0]=> 130 string(6) "piece1" 131 [1]=> 132 string(6) "piece2" 133 [2]=> 134 string(6) "piece3" 135 [3]=> 136 string(6) "piece4" 137 [4]=> 138 string(6) "piece5" 139 [5]=> 140 string(6) "piece6" 141} 142-- Iteration 6 -- 143array(0) { 144} 145-- Iteration 7 -- 146array(6) { 147 [0]=> 148 string(6) "piece1" 149 [1]=> 150 string(6) "piece2" 151 [2]=> 152 string(6) "piece3" 153 [3]=> 154 string(6) "piece4" 155 [4]=> 156 string(6) "piece5" 157 [5]=> 158 string(6) "piece6" 159} 160-- Iteration 8 -- 161array(0) { 162} 163-- Iteration 9 -- 164array(6) { 165 [0]=> 166 string(6) "piece1" 167 [1]=> 168 string(6) "piece2" 169 [2]=> 170 string(6) "piece3" 171 [3]=> 172 string(6) "piece4" 173 [4]=> 174 string(6) "piece5" 175 [5]=> 176 string(6) "piece6" 177} 178-- Iteration 10 -- 179 180Warning: explode() expects parameter 3 to be integer, array given in %s on line %d 181NULL 182-- Iteration 11 -- 183 184Warning: explode() expects parameter 3 to be integer, array given in %s on line %d 185NULL 186-- Iteration 12 -- 187 188Warning: explode() expects parameter 3 to be integer, array given in %s on line %d 189NULL 190-- Iteration 13 -- 191array(1) { 192 [0]=> 193 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 194} 195-- Iteration 14 -- 196array(1) { 197 [0]=> 198 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 199} 200-- Iteration 15 -- 201array(1) { 202 [0]=> 203 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 204} 205-- Iteration 16 -- 206array(1) { 207 [0]=> 208 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 209} 210-- Iteration 17 -- 211array(1) { 212 [0]=> 213 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 214} 215-- Iteration 18 -- 216array(1) { 217 [0]=> 218 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 219} 220-- Iteration 19 -- 221 222Warning: explode() expects parameter 3 to be integer, object given in %s on line %d 223NULL 224-- Iteration 20 -- 225 226Warning: explode() expects parameter 3 to be integer, resource given in %s on line %d 227NULL 228-- Iteration 21 -- 229array(1) { 230 [0]=> 231 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 232} 233-- Iteration 22 -- 234array(1) { 235 [0]=> 236 string(41) "piece1 piece2 piece3 piece4 piece5 piece6" 237} 238===Done=== 239