Current Location: Home> Latest Articles> Why is_double(1) returns false? Understand type checking

Why is_double(1) returns false? Understand type checking

gitbox 2025-05-27

Introduction to PHP's type system

To understand this behavior, we need to understand PHP's type system first. PHP is a weakly typed language that automatically converts in most cases. However, PHP still maintains clear data types internally:

  • Integer

  • Float type (double/float)

  • string

  • Boolean

  • Array

  • Object

  • Resource

  • NULL

This means that even if 1 and 1.0 are numerically equal, they are still different in type.


How does is_double work?

is_double() is a built-in function in PHP that detects whether a variable is of floating point type. The implementation logic is actually very simple, which is equivalent to checking whether the type identifier of the variable in memory is "floating point".

Let’s take a look at an example:

 $a = 1;
$b = 1.0;

var_dump(is_double($a)); // bool(false)
var_dump(is_double($b)); // bool(true)

In the above code, the variable $a is an integer, which is strictly identified as an "integer" in PHP, and $b is a floating point number, which will return true .

This explains the problem in the title of the article: is_double(1) returns false because the literal 1 is an integer, not a floating point type.


What will is_double('1.0') return?

You may also wonder how is_double() will behave when passing in a string that looks like a floating point number:

 var_dump(is_double('1.0')); // bool(false)

This result again shows that is_double() does not perform implicit type conversion. The string '1.0' is not automatically interpreted as a floating point number. is_double() will return true only if the internal type of the variable is already float.


The correct way to do type conversion

If your purpose is to determine whether a value can be interpreted as a floating point number, and not just relying on is_double() , you need to explicitly type conversion or use regular expression verification. For example:

 function is_numeric_float($value) {
    return is_numeric($value) && (strpos((string)$value, '.') !== false);
}

var_dump(is_numeric_float('1.0')); // bool(true)

Alternatively, you can use filter_var() for more flexible verification:

 var_dump(filter_var('1.0', FILTER_VALIDATE_FLOAT)); // float(1)

This type of method is more reliable in actual development than simply relying on is_double() , especially when processing data in user input or network requests.


Traps in practical applications

In some data verification scenarios, developers may mistakenly assume that is_double() can judge all values ​​that "look like a floating point number". For example, if you receive a JSON request from the front end, the field value is "1.0" . If you use is_double() to verify directly, it will be misjudged that the data format is incorrect.

The correct way is to combine is_numeric() with format verification, or manually convert field types when parsing JSON:

 $data = json_decode(file_get_contents('https://gitbox.net/data.json'), true);
$price = (float)$data['price'];
if (is_double($price)) {
    // Safe use of floating point numbers
}