1--TEST--
2Test explode() function : usage variations - test values for $delimiter argument
3--FILE--
4<?php
5
6/* Prototype  : array explode  ( string $delimiter  , string $string  [, int $limit  ] )
7 * Description: Split a string by string.
8 * Source code: ext/standard/string.c
9*/
10
11echo "*** Testing explode() function: with unexpected inputs for 'delimiter' argument ***\n";
12
13//get an unset variable
14$unset_var = 'string_val';
15unset($unset_var);
16
17//defining a class
18class sample  {
19  public function __toString() {
20    return "sample object";
21  }
22}
23
24//getting the resource
25$file_handle = fopen(__FILE__, "r");
26
27// array with different values for $delimeter
28$delimeters =  array (
29
30  // integer values
31  0,
32  1,
33  255,
34  256,
35  PHP_INT_MAX,
36  -PHP_INT_MAX,
37
38  // float values
39  10.5,
40  -20.5,
41  10.1234567e10,
42
43  // array values
44  array(),
45  array(0),
46  array(1, 2),
47
48  // boolean values
49  true,
50  false,
51  TRUE,
52  FALSE,
53
54  // null values
55  NULL,
56  null,
57
58  // objects
59  new sample(),
60
61  // resource
62  $file_handle,
63
64  // undefined variable
65  @$undefined_var,
66
67  // unset variable
68  @$unset_var
69);
70
71// loop through with each element of the $delimeters array to test explode() function
72$count = 1;
73$string = "piece1 piece2 piece3 piece4 piece5 piece6";
74$limit = 5;
75foreach($delimeters as $delimeter) {
76  echo "-- Iteration $count --\n";
77  var_dump( explode($delimeter, $string, $limit) );
78  $count ++;
79}
80
81fclose($file_handle);  //closing the file handle
82
83?>
84===Done===
85--EXPECTF--
86*** Testing explode() function: with unexpected inputs for 'delimiter' argument ***
87-- Iteration 1 --
88array(1) {
89  [0]=>
90  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
91}
92-- Iteration 2 --
93array(2) {
94  [0]=>
95  string(5) "piece"
96  [1]=>
97  string(35) " piece2 piece3 piece4 piece5 piece6"
98}
99-- Iteration 3 --
100array(1) {
101  [0]=>
102  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
103}
104-- Iteration 4 --
105array(1) {
106  [0]=>
107  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
108}
109-- Iteration 5 --
110array(1) {
111  [0]=>
112  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
113}
114-- Iteration 6 --
115array(1) {
116  [0]=>
117  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
118}
119-- Iteration 7 --
120array(1) {
121  [0]=>
122  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
123}
124-- Iteration 8 --
125array(1) {
126  [0]=>
127  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
128}
129-- Iteration 9 --
130array(1) {
131  [0]=>
132  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
133}
134-- Iteration 10 --
135
136Warning: explode() expects parameter 1 to be string, array given in %s on line %d
137NULL
138-- Iteration 11 --
139
140Warning: explode() expects parameter 1 to be string, array given in %s on line %d
141NULL
142-- Iteration 12 --
143
144Warning: explode() expects parameter 1 to be string, array given in %s on line %d
145NULL
146-- Iteration 13 --
147array(2) {
148  [0]=>
149  string(5) "piece"
150  [1]=>
151  string(35) " piece2 piece3 piece4 piece5 piece6"
152}
153-- Iteration 14 --
154
155Warning: explode(): Empty delimiter in %s on line %d
156bool(false)
157-- Iteration 15 --
158array(2) {
159  [0]=>
160  string(5) "piece"
161  [1]=>
162  string(35) " piece2 piece3 piece4 piece5 piece6"
163}
164-- Iteration 16 --
165
166Warning: explode(): Empty delimiter in %s on line %d
167bool(false)
168-- Iteration 17 --
169
170Warning: explode(): Empty delimiter in %s on line %d
171bool(false)
172-- Iteration 18 --
173
174Warning: explode(): Empty delimiter in %s on line %d
175bool(false)
176-- Iteration 19 --
177array(1) {
178  [0]=>
179  string(41) "piece1 piece2 piece3 piece4 piece5 piece6"
180}
181-- Iteration 20 --
182
183Warning: explode() expects parameter 1 to be string, resource given in %s on line %d
184NULL
185-- Iteration 21 --
186
187Warning: explode(): Empty delimiter in %s on line %d
188bool(false)
189-- Iteration 22 --
190
191Warning: explode(): Empty delimiter in %s on line %d
192bool(false)
193===Done===
194