Current Location: Home> Latest Articles> PHP Function is_float() Explained: How to Check If a Variable is a Float

PHP Function is_float() Explained: How to Check If a Variable is a Float

M66 2025-06-17

PHP Function is_float() Explained: How to Check If a Variable is a Float

PHP is a widely used server-side scripting language that supports various data types, including integers, strings, and floats. In the development process, it is common to validate the data type of variables. PHP provides several built-in functions to help developers check and handle different data types.

This article focuses on one commonly used type-checking function in PHP—is_float(). This function is used to check if a variable is a float. Let's dive into how to use it through example code.

Introduction to is_float() Function

The is_float() function takes a single parameter and returns a boolean value. If the parameter is a float, it returns true; otherwise, it returns false. We can better understand the function’s usage through the following example:


$var1 = 3.14;
$var2 = 7;
$var3 = "2.71";

if (is_float($var1)) {
    echo "$var1 is a float";
} else {
    echo "$var1 is not a float";
}

if (is_float($var2)) {
    echo "$var2 is a float";
} else {
    echo "$var2 is not a float";
}

if (is_float($var3)) {
    echo "$var3 is a float";
} else {
    echo "$var3 is not a float";
}
  

The output of the above code will be:


3.14 is a float
7 is not a float
2.71 is a float
  

As you can see from the example above, the is_float() function checks the data type of a variable and determines whether it is a float. If the variable is a float, the function returns true; otherwise, it returns false.

Usage Scenarios of the is_float() Function

It’s important to note that is_float() strictly checks the data type. It will only return true if the variable's data type is specifically float. If the variable is a string or integer that can be converted to a float, it will also return true. For instance, an integer like 7 will be automatically converted to the float 7.0 by PHP.

To better understand the application of is_float(), let’s look at an example with an array containing mixed data types:


$data = array(3.14, 2.71, "7.5", 5.23, "9.8");

foreach ($data as $value) {
    if (is_float($value)) {
        echo "$value is a float";
    } else {
        echo "$value is not a float";
    }
}
  

The output of the above code will be:


3.14 is a float
2.71 is a float
7.5 is not a float
5.23 is a float
9.8 is not a float
  

This example shows how is_float() helps us check the data type of each element in a mixed-type array, enabling us to handle each element accordingly. This is especially useful in development scenarios where we need to ensure data meets certain type requirements.

Conclusion

The PHP is_float() function is a commonly used tool for checking if a variable is a float. By using is_float(), developers can easily handle variables based on their data type during the development process. We hope this article helps you better understand PHP’s type-checking functions.