PHP float refers to the point in the numbers or numbers that contains a decimal point. In another word, we can name it “double” or “real numbers”.

Table Of Contents

PHP uses the float keyword with the variable name to define the decimal number. The size of PHP float is depending on the platforms, usually, the maximum float size is 1.8e308.

PHP Float Data Type

The following are the common formats for float.

1.16
-1.15
0.5 

PHP supports E notation or ( scientific notation ).

1.45E5 // 145000

Underscore is supported for float numbers in version 7.4 and prior

1_125.611 // 1125.611

Converting to PHP Float

To convert a data type to float you have more than one way there is a predefined function and casting.

<?php
    // Converting
    $data = floatval( "1.3665" ); 
    var_dump($data); // float(1.3665)
    
    // Casting 
    $data1= (float)"1.3665";
    var_dump($data1); // float(1.3665)
?>

If the value contains characters in the beginning the result will be zero( 0 ). Otherwise, if the string is in the middle of the number, it will convert only the numbers before the characters.

<?php

    $data = floatval( "coded1.3665" ); 
    var_dump($data); // float(0) 
    
    $data1 = floatval( "11.5Coded54" ); 
    var_dump($data1); // float(11.5) 
?>

Is Float function

The is_float function is to detect the current value already float type or else. So it is returning a boolean value.

<?php
   $float_data = 1.65;
   echo is_float( $float_data ); // 1 => refers to true value
?>

PHP Float Examples

PHP float would not work because it works approximately in the calculation unless you use the round function to get the value precisely.

<?php
   $val_x = 11 - 8.6;  // Result is 2.4;
   $val_y = 2.4;

   var_dump($val_x == $val_y); // bool(false)
?>

That’s happening because there is one value from the previous two variables that has another value. So we have to use a round predefined PHP function.

<?php
   $val_x = 11 - 8.6;  // Result is 2.4;
   $val_y = 2.4;

   var_dump(round($val_x, 2) == round($val_y, 2)); // bool(true)
?>

Conclusion

The float is a point decimal number. There are other references named it as a double or a real number. In other words, Underscore is supported for float numbers in version 7.4 and prior. Also supports E notation or ( scientific notation ).

The maximum float size is 1.8e308 and that is depending on the platform.

You learned also, how to convert the other types using cast and predefined built-in PHP function floatval(); and learned how to check if the current data type is float using the predefined built-ing function is_float();

To learn more details go to the PHP data Types tutorial. Or navigate this page.