1--TEST--
2Test mb_substr_count() function : usage variations - pass different data types as $needle arg
3--SKIPIF--
4<?php
5extension_loaded('mbstring') or die('skip');
6function_exists('mb_substr_count') or die("skip mb_substr_count() is not available in this build");
7?>
8--FILE--
9<?php
10/* Prototype  : int mb_substr_count(string $haystack, string $needle [, string $encoding])
11 * Description: Count the number of substring occurrences
12 * Source code: ext/mbstring/mbstring.c
13 */
14
15/*
16 * Pass different data types as $needle to mb_substr_count() to test behaviour
17 */
18
19echo "*** Testing mb_substr_count() : usage variations ***\n";
20
21// Initialise function arguments not being substituted (if any)
22$haystack = 'hello, world';
23
24//get an unset variable
25$unset_var = 10;
26unset ($unset_var);
27
28// get a class
29class classA
30{
31  public function __toString() {
32    return "world";
33  }
34}
35
36// heredoc string
37$heredoc = <<<EOT
38world
39EOT;
40
41// get a resource variable
42$fp = fopen(__FILE__, "r");
43
44// unexpected values to be passed to $needle argument
45$inputs = array(
46
47       // int data
48/*1*/  0,
49       1,
50       12345,
51       -2345,
52
53       // float data
54/*5*/  10.5,
55       -10.5,
56       12.3456789000e10,
57       12.3456789000E-10,
58       .5,
59
60       // null data
61/*10*/ NULL,
62       null,
63
64       // boolean data
65/*12*/ true,
66       false,
67       TRUE,
68       FALSE,
69
70       // empty data
71/*16*/ "",
72       '',
73
74       // string data
75/*18*/ "world",
76       'world',
77       $heredoc,
78
79       // object data
80/*21*/ new classA(),
81
82       // undefined data
83/*22*/ @$undefined_var,
84
85       // unset data
86/*23*/ @$unset_var,
87
88       // resource variable
89/*24*/ $fp
90);
91
92// loop through each element of $inputs to check the behavior of mb_substr_count()
93$iterator = 1;
94foreach($inputs as $input) {
95  echo "\n-- Iteration $iterator --\n";
96  var_dump( mb_substr_count($haystack, $input) );
97  $iterator++;
98};
99
100fclose($fp);
101
102echo "Done";
103?>
104--EXPECTF--
105*** Testing mb_substr_count() : usage variations ***
106
107-- Iteration 1 --
108int(0)
109
110-- Iteration 2 --
111int(0)
112
113-- Iteration 3 --
114int(0)
115
116-- Iteration 4 --
117int(0)
118
119-- Iteration 5 --
120int(0)
121
122-- Iteration 6 --
123int(0)
124
125-- Iteration 7 --
126int(0)
127
128-- Iteration 8 --
129int(0)
130
131-- Iteration 9 --
132int(0)
133
134-- Iteration 10 --
135
136Warning: mb_substr_count(): Empty substring in %s on line %d
137bool(false)
138
139-- Iteration 11 --
140
141Warning: mb_substr_count(): Empty substring in %s on line %d
142bool(false)
143
144-- Iteration 12 --
145int(0)
146
147-- Iteration 13 --
148
149Warning: mb_substr_count(): Empty substring in %s on line %d
150bool(false)
151
152-- Iteration 14 --
153int(0)
154
155-- Iteration 15 --
156
157Warning: mb_substr_count(): Empty substring in %s on line %d
158bool(false)
159
160-- Iteration 16 --
161
162Warning: mb_substr_count(): Empty substring in %s on line %d
163bool(false)
164
165-- Iteration 17 --
166
167Warning: mb_substr_count(): Empty substring in %s on line %d
168bool(false)
169
170-- Iteration 18 --
171int(1)
172
173-- Iteration 19 --
174int(1)
175
176-- Iteration 20 --
177int(1)
178
179-- Iteration 21 --
180int(1)
181
182-- Iteration 22 --
183
184Warning: mb_substr_count(): Empty substring in %s on line %d
185bool(false)
186
187-- Iteration 23 --
188
189Warning: mb_substr_count(): Empty substring in %s on line %d
190bool(false)
191
192-- Iteration 24 --
193
194Warning: mb_substr_count() expects parameter 2 to be string, resource given in %s on line %d
195NULL
196Done
197