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