1--TEST--
2Test 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
9echo "*** Testing is_string() with valid string values ***\n";
10// different valid strings
11
12/* string created using Heredoc (<<<) */
13$heredoc_string = <<<EOT
14This is string defined
15using heredoc.
16EOT;
17/* heredoc string with only numerics */
18$heredoc_numeric_string = <<<EOT
19123456 3993
204849 string
21EOT;
22/* null heardoc string */
23$heredoc_empty_string = <<<EOT
24EOT;
25$heredoc_null_string = <<<EOT
26NULL
27EOT;
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;
56foreach ($strings as $string ) {
57  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
58  var_dump( is_string($string) );
59}
60
61echo "\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
71this is heredoc string
72EOT;
73// unset the vars
74unset($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;
138foreach ($not_strings as $type ) {
139  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
140  var_dump( is_string($type) );
141}
142
143echo "\n*** Testing error conditions ***\n";
144//Zero argument
145var_dump( is_string() );
146
147//arguments more than expected
148var_dump( is_string("string", "test") );
149
150echo "Done\n";
151
152// close the resources used
153fclose($fp);
154closedir($dfp);
155
156?>
157--EXPECTF--
158*** Testing is_string() with valid string values ***
159-- Iteration 1 --
160bool(true)
161-- Iteration 2 --
162bool(true)
163-- Iteration 3 --
164bool(true)
165-- Iteration 4 --
166bool(true)
167-- Iteration 5 --
168bool(true)
169-- Iteration 6 --
170bool(true)
171-- Iteration 7 --
172bool(true)
173-- Iteration 8 --
174bool(true)
175-- Iteration 9 --
176bool(true)
177-- Iteration 10 --
178bool(true)
179-- Iteration 11 --
180bool(true)
181-- Iteration 12 --
182bool(true)
183-- Iteration 13 --
184bool(true)
185-- Iteration 14 --
186bool(true)
187-- Iteration 15 --
188bool(true)
189-- Iteration 16 --
190bool(true)
191-- Iteration 17 --
192bool(true)
193-- Iteration 18 --
194bool(true)
195-- Iteration 19 --
196bool(true)
197-- Iteration 20 --
198bool(true)
199-- Iteration 21 --
200bool(true)
201-- Iteration 22 --
202bool(true)
203
204*** Testing is_string() on non string values ***
205-- Iteration 1 --
206bool(false)
207-- Iteration 2 --
208bool(false)
209-- Iteration 3 --
210bool(false)
211-- Iteration 4 --
212bool(false)
213-- Iteration 5 --
214bool(false)
215-- Iteration 6 --
216bool(false)
217-- Iteration 7 --
218bool(false)
219-- Iteration 8 --
220bool(false)
221-- Iteration 9 --
222bool(false)
223-- Iteration 10 --
224bool(false)
225-- Iteration 11 --
226bool(false)
227-- Iteration 12 --
228bool(false)
229-- Iteration 13 --
230bool(false)
231-- Iteration 14 --
232bool(false)
233-- Iteration 15 --
234bool(false)
235-- Iteration 16 --
236bool(false)
237-- Iteration 17 --
238bool(false)
239-- Iteration 18 --
240bool(false)
241-- Iteration 19 --
242bool(false)
243-- Iteration 20 --
244bool(false)
245-- Iteration 21 --
246bool(false)
247-- Iteration 22 --
248bool(false)
249-- Iteration 23 --
250bool(false)
251-- Iteration 24 --
252bool(false)
253-- Iteration 25 --
254bool(false)
255-- Iteration 26 --
256bool(false)
257-- Iteration 27 --
258bool(false)
259-- Iteration 28 --
260bool(false)
261-- Iteration 29 --
262bool(false)
263-- Iteration 30 --
264bool(false)
265-- Iteration 31 --
266bool(false)
267-- Iteration 32 --
268bool(false)
269-- Iteration 33 --
270bool(false)
271-- Iteration 34 --
272bool(false)
273-- Iteration 35 --
274bool(false)
275-- Iteration 36 --
276bool(false)
277-- Iteration 37 --
278bool(false)
279-- Iteration 38 --
280bool(false)
281-- Iteration 39 --
282bool(false)
283-- Iteration 40 --
284bool(false)
285-- Iteration 41 --
286bool(false)
287-- Iteration 42 --
288bool(false)
289-- Iteration 43 --
290bool(false)
291-- Iteration 44 --
292bool(false)
293-- Iteration 45 --
294bool(false)
295
296*** Testing error conditions ***
297
298Warning: is_string() expects exactly 1 parameter, 0 given in %s on line %d
299bool(false)
300
301Warning: is_string() expects exactly 1 parameter, 2 given in %s on line %d
302bool(false)
303Done
304