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