1--TEST-- 2Test implode() function 3--FILE-- 4<?php 5echo "*** Testing implode() for basic operations ***\n"; 6$arrays = array( 7 array(1,2), 8 array(1.1,2.2), 9 array(array(2),array(1)), 10 array(false,true), 11 array(), 12 array("a","aaaa","b","bbbb","c","ccccccccccccccccccccc"), 13 array(NULL), 14); 15/* loop to output string with ', ' as $glue, using implode() */ 16foreach ($arrays as $array) { 17 var_dump(implode(', ', $array)); 18 var_dump($array); 19} 20 21echo "\n*** Testing implode() with variations of glue ***\n"; 22/* checking possible variations */ 23$pieces = array( 24 2, 25 0, 26 -639, 27 true, 28 "PHP", 29 false, 30 "", 31 " ", 32 "string\x00with\x00...\0" 33); 34$glues = array( 35 "TRUE", 36 true, 37 false, 38 array("key1", "key2"), 39 "", 40 " ", 41 "string\x00between", 42 -0, 43 '\0' 44); 45/* loop through to display a string containing all the array $pieces in the same order, 46 with the $glue string between each element */ 47$counter = 1; 48foreach($glues as $glue) { 49 echo "-- Iteration $counter --\n"; 50 try { 51 var_dump(implode($glue, $pieces)); 52 } catch (TypeError $exception) { 53 echo $exception->getMessage() . "\n"; 54 } 55 $counter++; 56} 57 58/* empty string */ 59echo "\n*** Testing implode() on empty string ***\n"; 60try { 61 implode(""); 62} catch (TypeError $e) { 63 echo $e->getMessage(), "\n"; 64} 65 66/* checking sub-arrays */ 67echo "\n*** Testing implode() on sub-arrays ***\n"; 68$sub_array = array(array(1,2,3,4), array(1 => "one", 2 => "two"), "PHP", 50); 69var_dump(implode("TEST", $sub_array)); 70try { 71 var_dump(implode(array(1, 2, 3, 4), $sub_array)); 72} catch (TypeError $exception) { 73 echo $exception->getMessage() . "\n"; 74} 75try { 76 var_dump(implode(2, $sub_array)); 77} catch (TypeError $exception) { 78 echo $exception->getMessage() . "\n"; 79} 80 81echo "\n*** Testing implode() on objects ***\n"; 82/* checking on objects */ 83class foo 84{ 85 function __toString() { 86 return "Object"; 87 } 88} 89 90$obj = new foo(); //creating new object 91$arr = array(); 92$arr[0] = &$obj; 93$arr[1] = &$obj; 94var_dump(implode(",", $arr)); 95var_dump($arr); 96 97/* Checking on resource types */ 98echo "\n*** Testing end() on resource type ***\n"; 99/* file type resource */ 100$file_handle = fopen(__FILE__, "r"); 101 102/* directory type resource */ 103$dir_handle = opendir( __DIR__ ); 104 105/* store resources in array for comparison */ 106$resources = array($file_handle, $dir_handle); 107 108var_dump(implode("::", $resources)); 109 110/* closing resource handles */ 111fclose($file_handle); 112closedir($dir_handle); 113 114echo "Done\n"; 115?> 116--EXPECTF-- 117*** Testing implode() for basic operations *** 118string(4) "1, 2" 119array(2) { 120 [0]=> 121 int(1) 122 [1]=> 123 int(2) 124} 125string(8) "1.1, 2.2" 126array(2) { 127 [0]=> 128 float(1.1) 129 [1]=> 130 float(2.2) 131} 132 133Warning: Array to string conversion in %s on line %d 134 135Warning: Array to string conversion in %s on line %d 136string(12) "Array, Array" 137array(2) { 138 [0]=> 139 array(1) { 140 [0]=> 141 int(2) 142 } 143 [1]=> 144 array(1) { 145 [0]=> 146 int(1) 147 } 148} 149string(3) ", 1" 150array(2) { 151 [0]=> 152 bool(false) 153 [1]=> 154 bool(true) 155} 156string(0) "" 157array(0) { 158} 159string(42) "a, aaaa, b, bbbb, c, ccccccccccccccccccccc" 160array(6) { 161 [0]=> 162 string(1) "a" 163 [1]=> 164 string(4) "aaaa" 165 [2]=> 166 string(1) "b" 167 [3]=> 168 string(4) "bbbb" 169 [4]=> 170 string(1) "c" 171 [5]=> 172 string(21) "ccccccccccccccccccccc" 173} 174string(0) "" 175array(1) { 176 [0]=> 177 NULL 178} 179 180*** Testing implode() with variations of glue *** 181-- Iteration 1 -- 182string(59) "2TRUE0TRUE-639TRUE1TRUEPHPTRUETRUETRUE TRUEstring%0with%0...%0" 183-- Iteration 2 -- 184string(35) "2101-639111PHP111 1string%0with%0...%0" 185-- Iteration 3 -- 186string(27) "20-6391PHP string%0with%0...%0" 187-- Iteration 4 -- 188implode(): Argument #1 ($separator) must be of type string, array given 189-- Iteration 5 -- 190string(27) "20-6391PHP string%0with%0...%0" 191-- Iteration 6 -- 192string(35) "2 0 -639 1 PHP string%0with%0...%0" 193-- Iteration 7 -- 194string(139) "2string%0between0string%0between-639string%0between1string%0betweenPHPstring%0betweenstring%0betweenstring%0between string%0betweenstring%0with%0...%0" 195-- Iteration 8 -- 196string(35) "2000-639010PHP000 0string%0with%0...%0" 197-- Iteration 9 -- 198string(43) "2\00\0-639\01\0PHP\0\0\0 \0string%0with%0...%0" 199 200*** Testing implode() on empty string *** 201implode(): If argument #1 ($separator) is of type string, argument #2 ($array) must be of type array, null given 202 203*** Testing implode() on sub-arrays *** 204 205Warning: Array to string conversion in %s on line %d 206 207Warning: Array to string conversion in %s on line %d 208string(27) "ArrayTESTArrayTESTPHPTEST50" 209implode(): Argument #1 ($separator) must be of type string, array given 210 211Warning: Array to string conversion in %s 212 213Warning: Array to string conversion in %s 214string(18) "Array2Array2PHP250" 215 216*** Testing implode() on objects *** 217string(13) "Object,Object" 218array(2) { 219 [0]=> 220 &object(foo)#%d (0) { 221 } 222 [1]=> 223 &object(foo)#%d (0) { 224 } 225} 226 227*** Testing end() on resource type *** 228string(%d) "Resource id #%d::Resource id #%d" 229Done 230