1--TEST--
2Test is_scalar() function
3--FILE--
4<?php
5/* Prototype: bool is_scalar ( mixed $var );
6 * Description: Finds whether a variable is a scalar (i.e integer, float, string or boolean)
7 */
8
9echo "*** Testing basic operations ***\n";
10$scalar_variables = array(
11  0,  // integers
12  1,
13  -45678,
14  0x5FF,  // hexadecimal as integer
15  0X566,
16  -0xAAF,
17  -0XCCF,
18  01234,  // octal as integer
19  -0126,
20
21  0.0,  // floats
22  -1.0,
23  1e5,
24  -1e7,
25  1.6E7,
26  475.e-8,
27  784.e+30,
28  98.45E+40,
29  .5E-40,
30
31  "",  // strings
32  '',
33  " ",
34  ' ',
35  "string",
36  'string',
37  "0",  // numeric as string
38  "40",
39  "50.696",
40  "0x534",
41  "0X534",
42
43  TRUE,  // boolean
44  FALSE,
45  true,
46  false
47);
48/* loop through each valid scalar variables in $scalar_variables
49   and see the working of is_scalar(), expected output: bool(true)
50*/
51$loop_counter = 1;
52foreach($scalar_variables as $scalar) {
53  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
54  var_dump( is_scalar($scalar) );
55}
56
57echo "\n*** Testing possible variations ***\n";
58// different scalar variables which are unset
59$int_var = 10;
60$float_var = 1e5;
61$string_var = "string";
62$boolean_var = true;
63$object = new stdclass;
64$array = array(10);
65$resource = opendir('.');
66unset($int_var, $float_var, $string_var, $boolean_var, $object, $array, $resource);
67
68// resources
69$fp = fopen(__FILE__, "r");
70$dfp = opendir(".");
71
72$variation_array = array(
73  NULL,
74  null,
75
76  array(),  // arrays
77  array(NULL),
78  array(true),
79  array(0),
80  array(1,2,3,4),
81
82  $fp,  // resources
83  $dfp,
84
85  new stdclass, // object
86
87  @$int_var,  // scalars that are unset
88  @$float_var,
89  @$string_var,
90  @$boolean_var,
91
92  @$array,   // non scalars that are unset
93  @$object,
94  @$resource,
95
96  @$undefined_var  // undefined variable
97);
98
99/* loop through each element of $variation_array to see the
100   working of is_scalar on non-scalar values, expected output: bool(false)
101*/
102$loop_counter = 1;
103foreach( $variation_array as $value ) {
104  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
105  var_dump( is_scalar($value) );
106}
107
108echo "\n*** Testing error conditions ***\n";
109// Zero arguments
110var_dump( is_scalar() );
111
112// Arguments more than expected
113var_dump( is_scalar( $scalar_variables[2], $scalar_variables[2]) );
114var_dump( is_scalar( new stdclass, new stdclass) );
115
116echo "Done\n";
117
118// close the resources used
119fclose($fp);
120closedir($dfp);
121
122?>
123--EXPECTF--
124*** Testing basic operations ***
125-- Iteration 1 --
126bool(true)
127-- Iteration 2 --
128bool(true)
129-- Iteration 3 --
130bool(true)
131-- Iteration 4 --
132bool(true)
133-- Iteration 5 --
134bool(true)
135-- Iteration 6 --
136bool(true)
137-- Iteration 7 --
138bool(true)
139-- Iteration 8 --
140bool(true)
141-- Iteration 9 --
142bool(true)
143-- Iteration 10 --
144bool(true)
145-- Iteration 11 --
146bool(true)
147-- Iteration 12 --
148bool(true)
149-- Iteration 13 --
150bool(true)
151-- Iteration 14 --
152bool(true)
153-- Iteration 15 --
154bool(true)
155-- Iteration 16 --
156bool(true)
157-- Iteration 17 --
158bool(true)
159-- Iteration 18 --
160bool(true)
161-- Iteration 19 --
162bool(true)
163-- Iteration 20 --
164bool(true)
165-- Iteration 21 --
166bool(true)
167-- Iteration 22 --
168bool(true)
169-- Iteration 23 --
170bool(true)
171-- Iteration 24 --
172bool(true)
173-- Iteration 25 --
174bool(true)
175-- Iteration 26 --
176bool(true)
177-- Iteration 27 --
178bool(true)
179-- Iteration 28 --
180bool(true)
181-- Iteration 29 --
182bool(true)
183-- Iteration 30 --
184bool(true)
185-- Iteration 31 --
186bool(true)
187-- Iteration 32 --
188bool(true)
189-- Iteration 33 --
190bool(true)
191
192*** Testing possible variations ***
193-- Iteration 1 --
194bool(false)
195-- Iteration 2 --
196bool(false)
197-- Iteration 3 --
198bool(false)
199-- Iteration 4 --
200bool(false)
201-- Iteration 5 --
202bool(false)
203-- Iteration 6 --
204bool(false)
205-- Iteration 7 --
206bool(false)
207-- Iteration 8 --
208bool(false)
209-- Iteration 9 --
210bool(false)
211-- Iteration 10 --
212bool(false)
213-- Iteration 11 --
214bool(false)
215-- Iteration 12 --
216bool(false)
217-- Iteration 13 --
218bool(false)
219-- Iteration 14 --
220bool(false)
221-- Iteration 15 --
222bool(false)
223-- Iteration 16 --
224bool(false)
225-- Iteration 17 --
226bool(false)
227-- Iteration 18 --
228bool(false)
229
230*** Testing error conditions ***
231
232Warning: is_scalar() expects exactly 1 parameter, 0 given in %s on line %d
233NULL
234
235Warning: is_scalar() expects exactly 1 parameter, 2 given in %s on line %d
236NULL
237
238Warning: is_scalar() expects exactly 1 parameter, 2 given in %s on line %d
239NULL
240Done
241