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