]> granicus.if.org Git - php/blob
a4ef7a47b1
[php] /
1 --TEST--
2 Test is_string() function
3 --FILE--
4 <?php
5 /* Prototype: bool is_string ( mixed $var );
6  * Description: Finds whether the given variable is a string
7  */
8
9 echo "*** Testing is_string() with valid string values ***\n";
10 // different valid strings
11
12 /* string created using Heredoc (<<<) */
13 $heredoc_string = <<<EOT
14 This is string defined
15 using heredoc.
16 EOT;
17 /* heredoc string with only numerics */
18 $heredoc_numeric_string = <<<EOT
19 123456 3993
20 4849 string
21 EOT;
22 /* null heardoc string */
23 $heredoc_empty_string = <<<EOT
24 EOT;
25 $heredoc_null_string = <<<EOT
26 NULL
27 EOT;
28
29 $strings = array(
30   "",
31   " ",
32   '',
33   ' ',
34   "string",
35   'string',
36   "NULL",
37   'null',
38   "FALSE",
39   'true',
40   "\x0b",
41   "\0",
42   '\0',
43   '\060',
44   "\070",
45   "0x55F",
46   "055",
47   "@#$#$%%$^^$%^%^$^&",
48   $heredoc_string,
49   $heredoc_numeric_string,
50   $heredoc_empty_string,
51   $heredoc_null_string
52 );
53 /* loop to check that is_string() recognizes different
54    strings, expected output bool(true) */
55 $loop_counter = 1;
56 foreach ($strings as $string ) {
57   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
58   var_dump( is_string($string) );
59 }
60
61 echo "\n*** Testing is_string() on non string values ***\n";
62
63 // get a resource type variable
64 $fp = fopen (__FILE__, "r");
65 $dfp = opendir ( __DIR__ );
66
67 // unset vars
68 $unset_string1 = "string";
69 $unset_string2 = 'string';
70 $unset_heredoc = <<<EOT
71 this is heredoc string
72 EOT;
73 // unset the vars
74 unset($unset_string1, $unset_string2, $unset_heredoc);
75
76 // other types in a array
77 $not_strings = array (
78   /* integers */
79   0,
80   1,
81   -1,
82   -0,
83   543915,
84   -5322,
85   0x0,
86   0x1,
87   0x55F,
88   -0xCCF,
89   0123,
90   -0654,
91   00,
92   01,
93
94   /* floats */
95   0.0,
96   1.0,
97   -1.0,
98   10.0000000000000000005,
99   .5e6,
100   -.5E7,
101   .5E+8,
102   -.5e+90,
103   1e5,
104   -1e5,
105   1E5,
106   -1E7,
107
108   /* objects */
109   new stdclass,
110
111   /* resources */
112   $fp,
113   $dfp,
114
115   /* arrays */
116   array(),
117   array(0),
118   array(1),
119   array(NULL),
120   array(null),
121   array("string"),
122   array(true),
123   array(TRUE),
124   array(false),
125   array(FALSE),
126   array(1,2,3,4),
127   array(1 => "One", "two" => 2),
128
129   /* undefined and unset vars */
130   @$unset_string1,
131   @$unset_string2,
132   @$unset_heredoc,
133   @$undefined_var
134 );
135 /* loop through the $not_strings to see working of
136    is_string() on non string types, expected output bool(false) */
137 $loop_counter = 1;
138 foreach ($not_strings as $type ) {
139   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
140   var_dump( is_string($type) );
141 }
142
143 echo "Done\n";
144
145 // close the resources used
146 fclose($fp);
147 closedir($dfp);
148
149 ?>
150 --EXPECT--
151 *** Testing is_string() with valid string values ***
152 -- Iteration 1 --
153 bool(true)
154 -- Iteration 2 --
155 bool(true)
156 -- Iteration 3 --
157 bool(true)
158 -- Iteration 4 --
159 bool(true)
160 -- Iteration 5 --
161 bool(true)
162 -- Iteration 6 --
163 bool(true)
164 -- Iteration 7 --
165 bool(true)
166 -- Iteration 8 --
167 bool(true)
168 -- Iteration 9 --
169 bool(true)
170 -- Iteration 10 --
171 bool(true)
172 -- Iteration 11 --
173 bool(true)
174 -- Iteration 12 --
175 bool(true)
176 -- Iteration 13 --
177 bool(true)
178 -- Iteration 14 --
179 bool(true)
180 -- Iteration 15 --
181 bool(true)
182 -- Iteration 16 --
183 bool(true)
184 -- Iteration 17 --
185 bool(true)
186 -- Iteration 18 --
187 bool(true)
188 -- Iteration 19 --
189 bool(true)
190 -- Iteration 20 --
191 bool(true)
192 -- Iteration 21 --
193 bool(true)
194 -- Iteration 22 --
195 bool(true)
196
197 *** Testing is_string() on non string values ***
198 -- Iteration 1 --
199 bool(false)
200 -- Iteration 2 --
201 bool(false)
202 -- Iteration 3 --
203 bool(false)
204 -- Iteration 4 --
205 bool(false)
206 -- Iteration 5 --
207 bool(false)
208 -- Iteration 6 --
209 bool(false)
210 -- Iteration 7 --
211 bool(false)
212 -- Iteration 8 --
213 bool(false)
214 -- Iteration 9 --
215 bool(false)
216 -- Iteration 10 --
217 bool(false)
218 -- Iteration 11 --
219 bool(false)
220 -- Iteration 12 --
221 bool(false)
222 -- Iteration 13 --
223 bool(false)
224 -- Iteration 14 --
225 bool(false)
226 -- Iteration 15 --
227 bool(false)
228 -- Iteration 16 --
229 bool(false)
230 -- Iteration 17 --
231 bool(false)
232 -- Iteration 18 --
233 bool(false)
234 -- Iteration 19 --
235 bool(false)
236 -- Iteration 20 --
237 bool(false)
238 -- Iteration 21 --
239 bool(false)
240 -- Iteration 22 --
241 bool(false)
242 -- Iteration 23 --
243 bool(false)
244 -- Iteration 24 --
245 bool(false)
246 -- Iteration 25 --
247 bool(false)
248 -- Iteration 26 --
249 bool(false)
250 -- Iteration 27 --
251 bool(false)
252 -- Iteration 28 --
253 bool(false)
254 -- Iteration 29 --
255 bool(false)
256 -- Iteration 30 --
257 bool(false)
258 -- Iteration 31 --
259 bool(false)
260 -- Iteration 32 --
261 bool(false)
262 -- Iteration 33 --
263 bool(false)
264 -- Iteration 34 --
265 bool(false)
266 -- Iteration 35 --
267 bool(false)
268 -- Iteration 36 --
269 bool(false)
270 -- Iteration 37 --
271 bool(false)
272 -- Iteration 38 --
273 bool(false)
274 -- Iteration 39 --
275 bool(false)
276 -- Iteration 40 --
277 bool(false)
278 -- Iteration 41 --
279 bool(false)
280 -- Iteration 42 --
281 bool(false)
282 -- Iteration 43 --
283 bool(false)
284 -- Iteration 44 --
285 bool(false)
286 -- Iteration 45 --
287 bool(false)
288 Done