1--TEST--
2Test 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
9echo "*** 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;
93foreach ($numerics as $num ) {
94  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
95  var_dump( is_numeric($num) );
96}
97
98echo "\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;
106unset ($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;
143foreach ($not_numerics as $type ) {
144  echo "-- Iteration $loop_counter --\n"; $loop_counter++;
145  var_dump( is_numeric($type) );
146}
147
148echo "\n*** Testing error conditions ***\n";
149//Zero argument
150var_dump( is_numeric() );
151
152//arguments more than expected
153var_dump( is_numeric("10", "20") );
154
155echo "Done\n";
156
157// close the resources used
158fclose($fp);
159closedir($dfp);
160
161?>
162--EXPECTF--
163*** Testing is_numeric() with valid numeric values ***
164-- Iteration 1 --
165bool(true)
166-- Iteration 2 --
167bool(true)
168-- Iteration 3 --
169bool(true)
170-- Iteration 4 --
171bool(true)
172-- Iteration 5 --
173bool(true)
174-- Iteration 6 --
175bool(true)
176-- Iteration 7 --
177bool(true)
178-- Iteration 8 --
179bool(true)
180-- Iteration 9 --
181bool(true)
182-- Iteration 10 --
183bool(true)
184-- Iteration 11 --
185bool(true)
186-- Iteration 12 --
187bool(true)
188-- Iteration 13 --
189bool(true)
190-- Iteration 14 --
191bool(true)
192-- Iteration 15 --
193bool(true)
194-- Iteration 16 --
195bool(true)
196-- Iteration 17 --
197bool(true)
198-- Iteration 18 --
199bool(true)
200-- Iteration 19 --
201bool(true)
202-- Iteration 20 --
203bool(true)
204-- Iteration 21 --
205bool(true)
206-- Iteration 22 --
207bool(true)
208-- Iteration 23 --
209bool(true)
210-- Iteration 24 --
211bool(true)
212-- Iteration 25 --
213bool(true)
214-- Iteration 26 --
215bool(true)
216-- Iteration 27 --
217bool(true)
218-- Iteration 28 --
219bool(true)
220-- Iteration 29 --
221bool(true)
222-- Iteration 30 --
223bool(true)
224-- Iteration 31 --
225bool(true)
226-- Iteration 32 --
227bool(true)
228-- Iteration 33 --
229bool(true)
230-- Iteration 34 --
231bool(true)
232-- Iteration 35 --
233bool(true)
234-- Iteration 36 --
235bool(true)
236-- Iteration 37 --
237bool(true)
238-- Iteration 38 --
239bool(true)
240-- Iteration 39 --
241bool(true)
242-- Iteration 40 --
243bool(true)
244-- Iteration 41 --
245bool(true)
246-- Iteration 42 --
247bool(true)
248-- Iteration 43 --
249bool(true)
250-- Iteration 44 --
251bool(true)
252-- Iteration 45 --
253bool(true)
254-- Iteration 46 --
255bool(true)
256-- Iteration 47 --
257bool(true)
258-- Iteration 48 --
259bool(true)
260-- Iteration 49 --
261bool(true)
262-- Iteration 50 --
263bool(true)
264-- Iteration 51 --
265bool(true)
266-- Iteration 52 --
267bool(true)
268-- Iteration 53 --
269bool(true)
270-- Iteration 54 --
271bool(true)
272-- Iteration 55 --
273bool(true)
274-- Iteration 56 --
275bool(true)
276-- Iteration 57 --
277bool(true)
278-- Iteration 58 --
279bool(true)
280-- Iteration 59 --
281bool(true)
282-- Iteration 60 --
283bool(true)
284-- Iteration 61 --
285bool(true)
286-- Iteration 62 --
287bool(true)
288-- Iteration 63 --
289bool(true)
290-- Iteration 64 --
291bool(true)
292-- Iteration 65 --
293bool(true)
294-- Iteration 66 --
295bool(true)
296-- Iteration 67 --
297bool(true)
298-- Iteration 68 --
299bool(true)
300-- Iteration 69 --
301bool(true)
302-- Iteration 70 --
303bool(true)
304-- Iteration 71 --
305bool(true)
306-- Iteration 72 --
307bool(true)
308-- Iteration 73 --
309bool(true)
310-- Iteration 74 --
311bool(true)
312-- Iteration 75 --
313bool(true)
314-- Iteration 76 --
315bool(true)
316
317*** Testing is_numeric() on non numeric types ***
318-- Iteration 1 --
319bool(false)
320-- Iteration 2 --
321bool(false)
322-- Iteration 3 --
323bool(false)
324-- Iteration 4 --
325bool(false)
326-- Iteration 5 --
327bool(false)
328-- Iteration 6 --
329bool(false)
330-- Iteration 7 --
331bool(false)
332-- Iteration 8 --
333bool(false)
334-- Iteration 9 --
335bool(false)
336-- Iteration 10 --
337bool(false)
338-- Iteration 11 --
339bool(false)
340-- Iteration 12 --
341bool(false)
342-- Iteration 13 --
343bool(false)
344-- Iteration 14 --
345bool(false)
346-- Iteration 15 --
347bool(false)
348-- Iteration 16 --
349bool(false)
350-- Iteration 17 --
351bool(false)
352-- Iteration 18 --
353bool(false)
354-- Iteration 19 --
355bool(false)
356-- Iteration 20 --
357bool(false)
358-- Iteration 21 --
359bool(false)
360-- Iteration 22 --
361bool(false)
362-- Iteration 23 --
363bool(false)
364-- Iteration 24 --
365bool(false)
366-- Iteration 25 --
367bool(false)
368-- Iteration 26 --
369bool(false)
370-- Iteration 27 --
371bool(false)
372-- Iteration 28 --
373bool(false)
374-- Iteration 29 --
375bool(false)
376
377*** Testing error conditions ***
378
379Warning: is_numeric() expects exactly 1 parameter, 0 given in %s on line %d
380NULL
381
382Warning: is_numeric() expects exactly 1 parameter, 2 given in %s on line %d
383NULL
384Done
385