A PHP float is a kind of number that has a decimal point, like 2.45 or 0.11. It’s also called a ‘double’ or ‘real number’.

It may be used in positive numbers or negative numbers, such as 3.14159 or -0.005.

In the following section, we will explain the precision of PHP floats and their limitations. Let’s move forward.

PHP Float Data Type

PHP Float Precision and Limitations

PHP float is used to represent numbers with fractional components.

Moreover, it is represented as double-precision floating-point numbers, which can store numbers with up to 14 digits of precision.

Usually, the range of float values in PHP depends on the platform, which spans from approximately -2.2250738585072E-308 to 1.7976931348623E+308.

So to show the minimum and maximum values of floats, use the following constants:

  • PHP_FLOAT_MIN
  • PHP_FLOAT_MAX

Let’s put them in a PHP example:

echo PHP_FLOAT_MIN // 2.2250738585072E-308
echo PHP_FLOAT_MAX // 1.7976931348623E+308

Anyway, here are some examples of floating numbers:

5.5
12.25
-10.17

Additionally, PHP converts very large floating numbers or very small floating numbers to scientific notation.

For example, a very small number like 0.000000000000000157 is written in scientific notation as 1.57E-16. And the very big number, like 1,797,693,134,862,315.7, is written as 1.7976931348623157E+15.

In PHP 7.4, you use underscores in long numbers to make them more readable. For example, you can write a number like 2_419.279_17.

Anyway, there are some considerations when you use comparisons in a floating number. Let’s see why it doesn’t work.

Why Close Isn’t Always Equal in Floating-Point Comparisons?

When you perform an operation between a decimal number and a floating number, you can use the result in a comparison, but be mindful of potential precision issues with floating-point numbers.

Check out the following example:

$x = 12.6;
$y= 10;
$z = $x - $y; // 2.6
Var_dump( $z == 2.6 ); // false

When you perform the operation $x - $y in PHP, with $x has 12.6 and $y has 10, one would logically expect the result to be exactly 2.6.

However, a surprising result occurs when this result is compared to the literal value of 2.6 using the == operator. The comparison is false.

The root cause of this discrepancy is the way the numbers are stored in memory, especially when the two numbers are floating or decimal. Basically, operating on a binary system struggles to represent certain decimal numbers with perfect accuracy.

So, the main number may be 2.5999999999999999 and not 2.6.

To solve this problem, just use the PHP built-in function round to refine floats with perfect accuracy.

$x = 12.6;
$y= 10;
$z = $x - $y; // 2.6
Var_dump( round($z, 2) == 2.6 ); // true

Let’s see more examples of floating numbers.

Examples of PHP Floats

PHP lets you do math with floats, which are numbers with decimals. You can add, multiply, and divide them. Like this:

$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:

// 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

Anyway, let’s summarize it in a few points

Wrapping Up

In this tutorial, you learned what a float is. You also understood some tricks for comparing floats and decimal numbers.

Anyway, here is a summary of what we covered:

In the first section, we basically defined the PHP float as numbers that have a decimal point in a specific position, like (3.14) or (-0.005).

They can be positive or negative. Additionally, it is precise and enables you to put up 14 digits after the decimal point.

You also understood how to get the minimum and maximum number in PHP float using these two constants: PHP_FLOAT_MIN and PHP_FLOAT_MAX.

You can also represent very large or small float numbers in scientific notation, such as by converting (0.000000000000000157) into (1.57E-16).

Thank you for reading. Happy Coding!