is_float

Checks whether a variable is of type float, without converting numeric strings.

Signature
is_float(mixed $value): bool
  • Added in PHP 4.0

Parameters

ParameterTypeDescription
$valuemixedThe variable being evaluated.

Return value

booltrue if $value is a float, false otherwise.

Changelog

  • PHP 8.0.0The alias is_real() was removed. is_double() remains a valid alias.

is_float() checks the type only. It is true for 27.25, for scientific notation like 1e7, and for the special values NAN, INF and -INF. It is false for the string '1.5', for the int 23, and for true.

is_double() is an alias and behaves identically. The old is_real() alias was removed in PHP 8.0.

Where floats appear unexpectedly

  • Division. 10 / 4 is a float, but 10 / 2 is an int — PHP returns an int when the division is exact. Use intdiv() when you always want an int.
  • Integer overflow. Anything above PHP_INT_MAX silently becomes a float, so a big ID from a database can quietly change type.
  • JSON. json_decode('1.0') gives a float while json_decode('1') gives an int, so the same logical value arrives as either type depending on how it was serialised.

When to reach for it

Use it when the distinction between int and float genuinely changes behaviour — formatting money, choosing between %d and %.2f, or deciding whether a value needs epsilon-based comparison. For validating user input, which arrives as strings, use is_numeric() or filter_var($v, FILTER_VALIDATE_FLOAT) instead.

Example

Try it yourself
Loading editor…
Press Run to execute the code.

When to use it

  • Choosing between %d and %.2f formatting when a value may arrive as either an int or a float
  • Detecting integer overflow, where a value above PHP_INT_MAX has silently become a float
  • Deciding whether a comparison needs an epsilon tolerance rather than a strict ===

Frequently asked questions

Why does is_float('1.5') return false?

Because '1.5' is a string, and is_float() checks the type rather than the content. Use is_numeric() or filter_var($v, FILTER_VALIDATE_FLOAT) for numeric strings.

What is the difference between is_float() and is_double()?

Nothing — is_double() is an alias for is_float(). The third alias, is_real(), was removed in PHP 8.0, so use is_float() in new code.

Is is_float(NAN) true?

Yes. NAN and INF are both of type float, so the type check passes. Add is_finite() or is_nan() when you need to know the value is usable.

More examples

Where floats come from

Exact division returns an int, so 10 / 2 is not a float while 10 / 4 is. The PHP_INT_MAX + 1 line is the important one: adding one to the largest int silently produces a float, which is how large identifiers lose precision.

Example · php
<?php
var_dump(is_float(10 / 2));
var_dump(is_float(10 / 4));
var_dump(is_float(intdiv(10, 4)));
var_dump(is_float(PHP_INT_MAX));
var_dump(is_float(PHP_INT_MAX + 1));
var_dump(is_float(0.1 + 0.2));

JSON changes the type

1 decodes as int, 1.0 as float, and the quoted "1.5" stays a string. is_float() separates them by type; is_numeric() is true for all three, which is why it is the right check for values that may still be strings.

Example · php
<?php
$data = json_decode('{"a": 1, "b": 1.0, "c": "1.5"}', true);

foreach ($data as $k => $v) {
    printf("%s: %-6s is_float=%-5s is_numeric=%s\n",
        $k,
        get_debug_type($v),
        var_export(is_float($v), true),
        var_export(is_numeric($v), true)
    );
}

NAN and INF pass the type check

sqrt(-1) is NAN and 1e308 * 10 overflows to INF, yet both are floats as far as is_float() is concerned. When a float has to be usable in arithmetic, follow the type check with is_finite().

Example · php
<?php
$results = [1.5, sqrt(-1), 1e308 * 10, 0.0];

foreach ($results as $r) {
    printf("%-8s float=%-5s finite=%-5s nan=%s\n",
        var_export($r, true),
        var_export(is_float($r), true),
        var_export(is_finite($r), true),
        var_export(is_nan($r), true)
    );
}

Discussion

  • Be the first to comment on this lesson.