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