In this article, we will explore the PHP float data type in detail, including its definition, range, and usage in code examples. We will also discuss some common operations that can be performed on float numbers in PHP.
Table Of Contents

What is a PHP float?
PHP float refers to the point in the numbers or numbers that contain a decimal point. In another word, we can name it “double” or “real numbers”.
A float is a data type that represents floating-point numbers. Floating-point numbers are used to represent real numbers with a fractional component, such as 3.14159 or -0.005.
In PHP, floats are represented as double-precision floating-point numbers, which can store numbers with up to 14 digits of precision.
The range of float values that can be represented in PHP is dependent on the underlying system architecture. In most systems, the minimum and maximum values of a float are -1.7976931348623157E+308 and 1.7976931348623157E+308, respectively.
## Declaring and Initializing Float Variables.
To declare a float variable in PHP, you can use the “float” or “double” keywords, or you can simply use the generic “number” data type. For example, the following code declares a float variable and initializes it with the value 3.14159:
<?php
$myFloat = 3.14159;
You can also use mathematical expressions to initialize float variables in PHP. For example:
<?php
$pi = 22 / 7; // 3.1428571428571
The result of the mathematical expression is automatically converted to a float and assigned to the $pi variable.
Performing Operations on Floats
PHP supports all the standard arithmetic operations on float numbers, including addition, subtraction, multiplication, and division. For example:
<?php
$a = 3.14;
$b = 2.71;
// Addition
$c = $a + $b; // 5.85
// Subtraction
$d = $a - $b; // 0.43
// Multiplication
$e = $a * $b; // 8.5094
// Division
$f = $a / $b; // 1.1590517223072
You can also perform more advanced operations on floats in PHP, such as exponentiation, modulus, and rounding. For example:
<?php
// Exponentiation
$g = pow($a, $b); // 36.977291374682
// Modulus
$h = $a % $b; // 0.43
// Rounding
$i = round($a, 1); // 3.1
$j = round($b, 1); // 2.7
As you can see, the pow()
function can be used to calculate the value of $a
raised to the power of $b
. The modulus operator (%) can be used to calculate the remainder of $a
divided by $b
.
Finally, the round()
function can be used to round float numbers to a specified number of decimal places.
<?php
$a = 0.1;
$b = 0.2;
$c = $a + $b;
echo $c; // 0.3
You might expect the value of $c to be 0.3,
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 contain 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.7976931348623157E+308 and 1.7976931348623157E+308, 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.