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