1--TEST--
2Testing floatval() and its alias doubleval() Functions
3--FILE--
4<?php
5echo "*** Testing floatval() with valid float values ***\n";
6// different valid  float values
7$valid_floats = array(
8                0.0,
9                1.0,
10                -1.0,
11                1.234,
12            -1.234,
13                1.2e3,
14            -1.2e3,
15        10.0000000000000000005,
16        10.5e+5,
17                1e5,
18            -1e5,
19                1e-5,
20        -1e-1,
21        1e+5,
22        -1e+5,
23        1E5,
24        -1E5,
25        1E+5,
26        -1E+5,
27        .5e+7,
28        -.5e+7
29);
30
31/* loop to check that floatval() recognizes different
32   float values, expected output:float value for valid floating point number */
33
34foreach ($valid_floats as $value ) {
35   var_dump( floatval($value) );
36}
37
38
39echo "\n*** Testing doubleval() with valid float values ***\n";
40/* loop to check that doubleval() also recognizes different
41   float values, expected output:float value for valid floating point number */
42
43foreach ($valid_floats as $value ) {
44   var_dump( doubleval($value) );
45}
46
47
48echo "\n*** Testing floatval() on non floating types ***\n";
49
50// get a resource type variable
51$fp = fopen (__FILE__, "r");
52fclose($fp);
53$dfp = opendir ( __DIR__ );
54closedir($dfp);
55
56// other types in an array
57$not_float_types = array (
58                   -2147483648, // max negative integer value
59                   2147483648,  // max positive integer value
60                   $fp,  // resource
61                   $dfp,
62                   "0.0", // string
63                   "1.0",
64               "-1.3e3",
65           "bob-1.3e3",
66                   "10 Some dollars",
67               "10.2 Some Dollars",
68               "10.0 dollar" + 1,
69           "10.0 dollar" + 1.0,
70                   "",
71                   true,
72                   NULL,
73                   null,
74                 );
75/* loop through the $not_float_types to see working of
76   floatval() on non float types, expected output: float value valid floating point numbers */
77foreach ($not_float_types as $type ) {
78   var_dump( floatval($type) );
79}
80
81
82echo "\n*** Testing doubleval() on non floating types ***\n";
83
84/* loop through the $not_float_types to see working of
85   doubleval() on non float types, expected output: float value valid floating point numbers */
86foreach ($not_float_types as $type ) {
87   var_dump( doubleval($type) );
88}
89
90echo "\nDone\n";
91
92
93?>
94--EXPECTF--
95*** Testing floatval() with valid float values ***
96float(0)
97float(1)
98float(-1)
99float(1.234)
100float(-1.234)
101float(1200)
102float(-1200)
103float(10)
104float(1050000)
105float(100000)
106float(-100000)
107float(1.0E-5)
108float(-0.1)
109float(100000)
110float(-100000)
111float(100000)
112float(-100000)
113float(100000)
114float(-100000)
115float(5000000)
116float(-5000000)
117
118*** Testing doubleval() with valid float values ***
119float(0)
120float(1)
121float(-1)
122float(1.234)
123float(-1.234)
124float(1200)
125float(-1200)
126float(10)
127float(1050000)
128float(100000)
129float(-100000)
130float(1.0E-5)
131float(-0.1)
132float(100000)
133float(-100000)
134float(100000)
135float(-100000)
136float(100000)
137float(-100000)
138float(5000000)
139float(-5000000)
140
141*** Testing floatval() on non floating types ***
142
143Warning: A non-numeric value encountered in %s on line %d
144
145Warning: A non-numeric value encountered in %s on line %d
146float(-2147483648)
147float(2147483648)
148float(5)
149float(6)
150float(0)
151float(1)
152float(-1300)
153float(0)
154float(10)
155float(10.2)
156float(11)
157float(11)
158float(0)
159float(1)
160float(0)
161float(0)
162
163*** Testing doubleval() on non floating types ***
164float(-2147483648)
165float(2147483648)
166float(5)
167float(6)
168float(0)
169float(1)
170float(-1300)
171float(0)
172float(10)
173float(10.2)
174float(11)
175float(11)
176float(0)
177float(1)
178float(0)
179float(0)
180
181Done
182