Backend Development 5 min read

Using PHP's is_float() Function to Check for Floating-Point Numbers

This tutorial explains PHP's is_float() function, demonstrates its boolean return behavior with simple and complex code examples, and discusses type‑checking nuances such as strict float detection and automatic conversion of integers and strings.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's is_float() Function to Check for Floating-Point Numbers

PHP is a widely used server‑side scripting language that supports various data types such as integers, strings, and floating‑point numbers. During development it is often necessary to validate a variable’s type, and PHP provides built‑in functions for type checking.

This article focuses on the commonly used type‑checking function is_float() , which determines whether a variable is a floating‑point number. The function accepts a single argument and returns true if the argument’s type is float , otherwise false . Simple usage is shown below.

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

if (is_float($var1)) {
    echo "$var1 是一个浮点数
";
} else {
    echo "$var1 不是一个浮点数
";
}

if (is_float($var2)) {
    echo "$var2 是一个浮点数
";
} else {
    echo "$var2 不是一个浮点数
";
}

if (is_float($var3)) {
    echo "$var3 是一个浮点数
";
} else {
    echo "$var3 不是一个浮点数
";
}

The script outputs:

3.14 是一个浮点数
7 不是一个浮点数
2.71 是一个浮点数

From this example we see that is_float() returns true only when the variable’s actual type is float . PHP performs strict type checking, but it will also return true for strings or integers that can be implicitly converted to a float.

To illustrate a more complex scenario, the following code iterates over an array containing mixed types and uses is_float() to identify floating‑point values.

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

foreach ($data as $value) {
    if (is_float($value)) {
        echo "$value 是一个浮点数
";
    } else {
        echo "$value 不是一个浮点数
";
    }
}

The output of the above script is:

3.14 是一个浮点数
2.71 是一个浮点数
7.5 不是一个浮点数
5.23 是一个浮点数
9.8 不是一个浮点数

These examples demonstrate how is_float() can be used to examine each element in a mixed‑type collection such as the $data array, enabling developers to handle values according to their actual type.

In summary, the PHP function is_float() provides a straightforward way to check whether a variable is a floating‑point number, allowing developers to perform appropriate type‑specific processing during development.

PHP8 video tutorial

Scan the QR code to receive free learning materials

backendProgrammingphptype checkingis_float
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.