PHP float Keyword
last modified November 25, 2026
The PHP float keyword names the floating-point type. A float holds a number with a fractional part and is stored as an IEEE 754 double-precision value, so it can represent very large and very small magnitudes at the cost of exactness.
This article covers float literals, casts, type declarations, strict typing, the special values INF and NAN, comparison, and formatting. Precision and money handling are covered in the floating-point numbers tutorial.
Basic definitions
A float is a number with a fractional part. PHP stores it in binary as an IEEE 754 double-precision value: one sign bit, an 11-bit exponent, and a 53-bit significand. That gives about 15 to 17 significant decimal digits and a huge exponent range, but not exact decimal fractions.
The type name in declarations and casts is float. The word double is an alias for the same type, and is_double still works, but the (double) cast is deprecated since PHP 8.5, so write (float). In a type declaration double is not recognized as a type at all: PHP reads it as a class name and warns about it.
Floats are approximations. Values such as 0.1 or 19.99 have no exact binary representation, so arithmetic can produce a result that differs from the decimal value you expect. The constants PHP_FLOAT_DIG, PHP_FLOAT_EPSILON, and PHP_FLOAT_MAX describe the current platform. The floating-point numbers article covers precision, rounding, and money calculations in more depth.
Declaring floats
A float literal contains a decimal point, an exponent, or both. The first example uses ordinary decimal notation, a negative value, and scientific notation.
<?php declare(strict_types=1); $price = 19.99; $temperature = -3.5; $distance = 1.5e3; $ratio = 2E-3; echo "Price: $price\n"; echo "Temperature: $temperature\n"; echo "Distance: $distance m\n"; echo "Ratio: $ratio\n"; echo "Type: ", get_debug_type($price), "\n"; var_dump($price);
1.5e3 means 1.5 times 10 to the third power, so it prints as 1500. 2E-3 is 0.002. PHP does not keep the notation: every literal becomes the same internal float. get_debug_type confirms the type, and var_dump shows both the type and the stored value.
$ php float_declare.php Price: 19.99 Temperature: -3.5 Distance: 1500 m Ratio: 0.002 Type: float float(19.99)
Float literals
Several spellings produce a float: 15.0, .5, 5., and a literal with underscores between digits. An exponent may also overflow the representable range, which produces the special value INF instead of an error.
<?php
declare(strict_types=1);
$values = [15.0, .5, 5., 1_000.25, 6.022e23];
foreach ($values as $value) {
var_dump($value);
}
var_dump(1e1000);
echo "PHP_FLOAT_DIG: ", PHP_FLOAT_DIG, "\n";
echo "PHP_FLOAT_EPSILON: ", PHP_FLOAT_EPSILON, "\n";
echo "PHP_FLOAT_MAX: ", PHP_FLOAT_MAX, "\n";
The trailing .0 matters: 15.0 is a float while 15 is an integer. .5 and 5. are accepted, but most style guides prefer a digit on both sides of the point. 6.022e23 is printed in exponent form because the value is too large for ordinary notation to stay readable.
PHP_FLOAT_DIG is the number of decimal digits that survive a round trip, 15 on this platform. PHP_FLOAT_EPSILON is the smallest difference at 1.0, and PHP_FLOAT_MAX is the largest finite value. A literal beyond that range becomes INF, and an operation that exceeds the range does the same.
$ php float_literals.php float(15) float(0.5) float(5) float(1000.25) float(6.022E+23) float(INF) PHP_FLOAT_DIG: 15 PHP_FLOAT_EPSILON: 2.2204460492503E-16 PHP_FLOAT_MAX: 1.7976931348623E+308
Casting to float
The (float) cast and the floatval function convert other types to a float. The rules for strings are the important part.
<?php
declare(strict_types=1);
$values = [42, "3.14", "2.5 meters", "abc", true, false, null];
foreach ($values as $value) {
printf("%-12s => ", get_debug_type($value));
var_dump((float) $value);
}
var_dump(floatval("1.5e3"));
var_dump(filter_var("3.14", FILTER_VALIDATE_FLOAT));
var_dump(filter_var("3,14", FILTER_VALIDATE_FLOAT));
An integer becomes a float with the same value. A numeric string is read up to the first character that cannot belong to a number, so "2.5 meters" becomes 2.5 while "abc" becomes 0.0. true becomes 1.0, and false and null become 0.0.
Casting is not validation: a bad string silently becomes 0.0, which may look like a real price. When the value comes from a user or a file, use filter_var($value, FILTER_VALIDATE_FLOAT), which returns the float or false. It rejects "3,14", so a decimal comma has to be normalized before validation.
$ php float_casting.php int => float(42) string => float(3.14) string => float(2.5) string => float(0) bool => float(1) bool => float(0) null => float(0) float(1500) float(3.14) bool(false)
Float type declarations
A parameter or return type of float accepts integers as well, even with declare(strict_types=1). This widening rule exists because every integer has an exact float counterpart in the common range, so passing 2 where a float is expected is safe. Strings are not converted in strict mode.
<?php
declare(strict_types=1);
function circleArea(float $radius): float
{
return M_PI * $radius ** 2;
}
function half(int $value): float
{
return $value / 2;
}
echo circleArea(2), "\n";
echo half(5), "\n";
try {
echo circleArea("2"), "\n";
} catch (TypeError $e) {
echo $e->getMessage(), "\n";
}
circleArea(2) passes an integer to a float parameter and works, while circleArea("2") raises a TypeError. Returning an integer from a function declared to return float is allowed too, which is why half(5) prints 2.5: the division already produces a float. The catch block is present only to show the message without stopping the script; the int keyword article explains strict and weak typing in more detail.
$ php float_declarations.php 12.566370614359 2.5 circleArea(): Argument #1 ($radius) must be of type float, string given, called in /app/float_declarations.php on line 19
Infinity and NaN
Floating-point arithmetic has three special values: positive infinity, negative infinity, and not-a-number. The division operator throws a DivisionByZeroError since PHP 8, so the fdiv function is the explicit way to get the IEEE 754 result.
<?php
declare(strict_types=1);
$values = [
'positive' => fdiv(1, 0),
'negative' => fdiv(-1, 0),
'undefined' => fdiv(0, 0),
'sqrt(-1)' => sqrt(-1),
];
foreach ($values as $label => $value) {
printf("%-10s %-6s finite=%s nan=%s\n",
$label,
var_export($value, true),
var_export(is_finite($value), true),
var_export(is_nan($value), true)
);
}
var_dump(INF > PHP_FLOAT_MAX);
var_dump(NAN == NAN);
fdiv(1, 0) returns INF, fdiv(-1, 0) returns -INF, and fdiv(0, 0) returns NAN. A negative square root also produces NAN. is_finite is false for all of them, and is_nan is true only for NAN.
NAN is not equal to anything, including itself, so a comparison with == cannot detect it; use is_nan. INF compares greater than every finite value. These values can travel through calculations without an error, so validate results before storing or displaying them.
$ php float_special.php positive INF finite=false nan=false negative -INF finite=false nan=false undefined NAN finite=false nan=true sqrt(-1) NAN finite=false nan=true bool(true) bool(false)
Comparing floats
Because decimal fractions are stored approximately, two calculations that should produce the same number may differ in the last bits. The next example checks the classic 0.1 + 0.2 case.
<?php
declare(strict_types=1);
$sum = 0.1 + 0.2;
var_dump($sum);
var_dump($sum == 0.3);
var_dump(abs($sum - 0.3) < PHP_FLOAT_EPSILON);
var_dump(round($sum, 2) == 0.3);
printf("%.17f\n", $sum);
printf("%.2f\n", $sum);
The sum is 0.30000000000000004, not 0.3, so == returns false. Comparing the absolute difference with PHP_FLOAT_EPSILON works here, but epsilon is an absolute value tied to 1.0; for larger numbers the tolerance has to grow with the magnitude. Rounding both sides to the number of digits that matter is another practical approach.
Choose the method from the problem: a tolerance for measurements, integer cents for money, and round only for display. The floating-point numbers article compares the alternatives in detail.
$ php float_compare.php float(0.30000000000000004) bool(false) bool(true) bool(true) 0.30000000000000004 0.30
Formatting floats
A float is stored as a binary value, but it is usually shown with a fixed number of decimals. number_format adds thousands separators, and printf controls the width and precision.
<?php
declare(strict_types=1);
$amount = 1234567.891;
echo number_format($amount, 2, ".", ","), "\n";
printf("%.3f\n", 2 / 3);
printf("%08.2f\n", 42.5);
echo round(2.5), " ", round(-2.5), "\n";
number_format returns a string, so it belongs at the output boundary, not in the stored value. %.3f prints three decimals, and %08.2f pads the result with zeros to eight characters. round returns a float and rounds halves away from zero, so 2.5 becomes 3 and -2.5 becomes -3.
Formatting does not fix a value: the rounded string is a display of the original float. Keep the full value for calculations and format only when printing.
$ php float_format.php 1,234,567.89 0.667 00042.50 3 -3
Best practices
Use floats for measurements and scientific values, and keep these rules in mind.
Source
PHP manual: floating point numbers
This article covered the PHP float type: literals, casts, declarations, special values, comparison, and formatting. Continue with PHP int keyword for the integer type, or read PHP floating-point numbers for precision and rounding in depth.
Author
List all PHP tutorials.