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