Backend Development 4 min read

Master PHP’s is_float(): Detect Floats with Simple Code Examples

This article explains PHP's is_float() function, how it strictly checks for float types, demonstrates basic and array‑based examples, and highlights important nuances such as automatic conversion of integers, helping developers reliably identify floating‑point values in their code.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Master PHP’s is_float(): Detect Floats with Simple Code Examples

PHP is a widely used server‑side scripting language that supports various data types. In development we often need to verify variable types, and PHP provides built‑in functions for type checking.

This article focuses on the commonly used type‑checking function

is_float()

, which checks whether a variable is a floating‑point number.

The

is_float()

function accepts one argument and returns a boolean: true if the variable is of type float, false otherwise. Example:

$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 output is:

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

Note that

is_float()

strictly checks the variable’s type; it returns true only when the type is float. If the variable is a string or integer that can be converted to a float, the function still returns true because PHP automatically converts integers to the nearest float.

A more complex example demonstrates using

is_float()

on each element of an array:

$data = array(3.14, 2.71, "7.5", 5.23, "9.8");
foreach ($data as $value) {
    if (is_float($value)) {
        echo "$value 是一个浮点数<br>";
    } else {
        echo "$value 不是一个浮点数<br>";
    }
}

The result shows which elements are floats and which are not, illustrating a typical use case for mixed‑type data collections.

In summary, the PHP

is_float()

function is a convenient way to determine whether a variable is a floating‑point number and can be applied in various development scenarios.

backend developmentphptype checkingis_floatfloat detection
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.