]> granicus.if.org Git - php/blob
491aa5d15b
[php] /
1 --TEST--
2 Test is_numeric() function
3 --FILE--
4 <?php
5 /* Prototype: bool is_numeric ( mixed $var );
6  * Description: Finds whether a variable is a number or a numeric string
7  */
8
9 echo "*** Testing is_numeric() with valid numeric values ***\n";
10 // different valid numeric  values
11 $numerics = array(
12   0,
13   1,
14   -1,
15   -0,
16   +0,
17   0.0,
18   -0.0,
19   +0.0,
20   1.0,
21   -1.0,
22   +1.0,
23   .5,
24   -.5,
25   +.5,
26   -.5e-2,
27   .5e-2,
28   +.5e-2,
29   +.5E+2,
30   0.70000000,
31   +0.70000000,
32   -0.70000000,
33   1234567890123456,
34   -1234567890123456,
35   984847472827282718178,
36   -984847472827282718178,
37   123.56e30,
38   123.56E30,
39   426.45e-30,
40   5657.3E-40,
41   3486.36e+40,
42   3486.36E+90,
43   -3486.36E+10,
44   -3486.36e+80,
45   -426.45e-50,
46   -426.45E-99,
47   1e2,
48   -1e2,
49   -1e-2,
50   +1e2,
51   +1e+2,
52   +1e-2,
53   +1e+2,
54   2245555555555555.444,
55   1.444444444444444444,
56   0xff, // hexa decimal numbers
57   0xFF,
58   //0x1111111111111111111111,
59   -0x1111111,
60   +0x6698319,
61   01000000000000000000000,
62   0123,
63   0345,
64   -0200001,
65   -0200001.7,
66   0200001.7,
67   +0200001,
68   +0200001.7,
69   +0200001.7,
70   2.00000000000000000000001, // a float value with more precision points
71   "1",  // numeric in the form of string
72   "-1",
73   "1e2",
74   " 1",
75   "2974394749328742328432",
76   "-1e-2",
77   '1',
78   '-1',
79   '1e2',
80   ' 1',
81   '2974394749328742328432',
82   '-1e-2',
83   "0123",
84   '0123',
85   "-0123",
86   "+0123",
87   '-0123',
88   '+0123'
89 );
90 /* loop to check that is_numeric() recognizes different
91    numeric values, expected output: bool(true) */
92 $loop_counter = 1;
93 foreach ($numerics as $num ) {
94   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
95   var_dump( is_numeric($num) );
96 }
97
98 echo "\n*** Testing is_numeric() on non numeric types ***\n";
99
100 // get a resource type variable
101 $fp = fopen (__FILE__, "r");
102 $dfp = opendir ( __DIR__ );
103
104 // unset variable
105 $unset_var = 10.5;
106 unset ($unset_var);
107
108 // other types in a array
109 $not_numerics = array(
110   "0x80001",
111   "-0x80001",
112   "+0x80001",
113   "-0x80001.5",
114   "0x80001.5",
115   new stdclass, // object
116   $fp,  // resource
117   $dfp,
118   array(),
119   array("string"),
120   "",
121   "1 ",
122   "- 1",
123   "1.2.4",
124   "1e7.6",
125   "3FF",
126   "20 test",
127   "3.6test",
128   "1,000",
129   "NULL",
130   "true",
131   true,
132   NULL,
133   null,
134   TRUE,
135   FALSE,
136   false,
137   @$unset_var, // unset variable
138   @$undefined_var
139 );
140 /* loop through the $not_numerics to see working of
141    is_numeric() on non numeric values, expected output: bool(false) */
142 $loop_counter = 1;
143 foreach ($not_numerics as $type ) {
144   echo "-- Iteration $loop_counter --\n"; $loop_counter++;
145   var_dump( is_numeric($type) );
146 }
147
148 echo "Done\n";
149
150 // close the resources used
151 fclose($fp);
152 closedir($dfp);
153
154 ?>
155 --EXPECTF--
156 *** Testing is_numeric() with valid numeric values ***
157 -- Iteration 1 --
158 bool(true)
159 -- Iteration 2 --
160 bool(true)
161 -- Iteration 3 --
162 bool(true)
163 -- Iteration 4 --
164 bool(true)
165 -- Iteration 5 --
166 bool(true)
167 -- Iteration 6 --
168 bool(true)
169 -- Iteration 7 --
170 bool(true)
171 -- Iteration 8 --
172 bool(true)
173 -- Iteration 9 --
174 bool(true)
175 -- Iteration 10 --
176 bool(true)
177 -- Iteration 11 --
178 bool(true)
179 -- Iteration 12 --
180 bool(true)
181 -- Iteration 13 --
182 bool(true)
183 -- Iteration 14 --
184 bool(true)
185 -- Iteration 15 --
186 bool(true)
187 -- Iteration 16 --
188 bool(true)
189 -- Iteration 17 --
190 bool(true)
191 -- Iteration 18 --
192 bool(true)
193 -- Iteration 19 --
194 bool(true)
195 -- Iteration 20 --
196 bool(true)
197 -- Iteration 21 --
198 bool(true)
199 -- Iteration 22 --
200 bool(true)
201 -- Iteration 23 --
202 bool(true)
203 -- Iteration 24 --
204 bool(true)
205 -- Iteration 25 --
206 bool(true)
207 -- Iteration 26 --
208 bool(true)
209 -- Iteration 27 --
210 bool(true)
211 -- Iteration 28 --
212 bool(true)
213 -- Iteration 29 --
214 bool(true)
215 -- Iteration 30 --
216 bool(true)
217 -- Iteration 31 --
218 bool(true)
219 -- Iteration 32 --
220 bool(true)
221 -- Iteration 33 --
222 bool(true)
223 -- Iteration 34 --
224 bool(true)
225 -- Iteration 35 --
226 bool(true)
227 -- Iteration 36 --
228 bool(true)
229 -- Iteration 37 --
230 bool(true)
231 -- Iteration 38 --
232 bool(true)
233 -- Iteration 39 --
234 bool(true)
235 -- Iteration 40 --
236 bool(true)
237 -- Iteration 41 --
238 bool(true)
239 -- Iteration 42 --
240 bool(true)
241 -- Iteration 43 --
242 bool(true)
243 -- Iteration 44 --
244 bool(true)
245 -- Iteration 45 --
246 bool(true)
247 -- Iteration 46 --
248 bool(true)
249 -- Iteration 47 --
250 bool(true)
251 -- Iteration 48 --
252 bool(true)
253 -- Iteration 49 --
254 bool(true)
255 -- Iteration 50 --
256 bool(true)
257 -- Iteration 51 --
258 bool(true)
259 -- Iteration 52 --
260 bool(true)
261 -- Iteration 53 --
262 bool(true)
263 -- Iteration 54 --
264 bool(true)
265 -- Iteration 55 --
266 bool(true)
267 -- Iteration 56 --
268 bool(true)
269 -- Iteration 57 --
270 bool(true)
271 -- Iteration 58 --
272 bool(true)
273 -- Iteration 59 --
274 bool(true)
275 -- Iteration 60 --
276 bool(true)
277 -- Iteration 61 --
278 bool(true)
279 -- Iteration 62 --
280 bool(true)
281 -- Iteration 63 --
282 bool(true)
283 -- Iteration 64 --
284 bool(true)
285 -- Iteration 65 --
286 bool(true)
287 -- Iteration 66 --
288 bool(true)
289 -- Iteration 67 --
290 bool(true)
291 -- Iteration 68 --
292 bool(true)
293 -- Iteration 69 --
294 bool(true)
295 -- Iteration 70 --
296 bool(true)
297 -- Iteration 71 --
298 bool(true)
299 -- Iteration 72 --
300 bool(true)
301 -- Iteration 73 --
302 bool(true)
303 -- Iteration 74 --
304 bool(true)
305 -- Iteration 75 --
306 bool(true)
307 -- Iteration 76 --
308 bool(true)
309
310 *** Testing is_numeric() on non numeric types ***
311 -- Iteration 1 --
312 bool(false)
313 -- Iteration 2 --
314 bool(false)
315 -- Iteration 3 --
316 bool(false)
317 -- Iteration 4 --
318 bool(false)
319 -- Iteration 5 --
320 bool(false)
321 -- Iteration 6 --
322 bool(false)
323 -- Iteration 7 --
324 bool(false)
325 -- Iteration 8 --
326 bool(false)
327 -- Iteration 9 --
328 bool(false)
329 -- Iteration 10 --
330 bool(false)
331 -- Iteration 11 --
332 bool(false)
333 -- Iteration 12 --
334 bool(false)
335 -- Iteration 13 --
336 bool(false)
337 -- Iteration 14 --
338 bool(false)
339 -- Iteration 15 --
340 bool(false)
341 -- Iteration 16 --
342 bool(false)
343 -- Iteration 17 --
344 bool(false)
345 -- Iteration 18 --
346 bool(false)
347 -- Iteration 19 --
348 bool(false)
349 -- Iteration 20 --
350 bool(false)
351 -- Iteration 21 --
352 bool(false)
353 -- Iteration 22 --
354 bool(false)
355 -- Iteration 23 --
356 bool(false)
357 -- Iteration 24 --
358 bool(false)
359 -- Iteration 25 --
360 bool(false)
361 -- Iteration 26 --
362 bool(false)
363 -- Iteration 27 --
364 bool(false)
365 -- Iteration 28 --
366 bool(false)
367 -- Iteration 29 --
368 bool(false)
369 Done