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