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