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 ( dirname(__FILE__) ); 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 *** 158float(-2147483648) 159float(2147483648) 160float(5) 161float(6) 162float(0) 163float(1) 164float(-1300) 165float(0) 166float(10) 167float(10.2) 168float(11) 169float(11) 170float(0) 171float(1) 172float(0) 173float(0) 174 175*** Testing doubleval() on non floating types *** 176float(-2147483648) 177float(2147483648) 178float(5) 179float(6) 180float(0) 181float(1) 182float(-1300) 183float(0) 184float(10) 185float(10.2) 186float(11) 187float(11) 188float(0) 189float(1) 190float(0) 191float(0) 192 193*** Testing error conditions *** 194 195Warning: floatval() expects exactly 1 parameter, 0 given in %s on line %d 196NULL 197 198Warning: doubleval() expects exactly 1 parameter, 0 given in %s on line %d 199NULL 200 201Warning: floatval() expects exactly 1 parameter, 2 given in %s on line %d 202NULL 203 204Warning: doubleval() expects exactly 1 parameter, 2 given in %s on line %d 205NULL 206 207Done 208