Using PHP is_float() to Detect Floating‑Point Numbers

This article explains PHP's is_float() function, covering its syntax, behavior, practical usage, and provides multiple code examples demonstrating how to detect floating‑point numbers and combine the check with conditional logic in real‑world applications.

php Courses
php Courses
php Courses
Using PHP is_float() to Detect Floating‑Point Numbers

In PHP programming, the is_float() function is used to check whether a variable is a floating‑point number (i.e., a decimal). This article details the syntax, usage, and example code of is_float().

1. Function Syntax

is_float ( mixed $var ) : bool

The is_float() function accepts a single parameter $var, which may be of any type. It returns a boolean value: true if $var is of float type, otherwise false.

2. Function Usage

The is_float() function is commonly used to determine whether a variable is a float, which is especially useful during numeric calculations and type checking. It helps avoid errors or logical problems caused by incorrect variable types.

3. Code Examples

Below are some example codes that demonstrate how to use the is_float() function:

Example 1: Determine if a variable is a float

$var1 = 3.14;
$var2 = 5;
$var3 = "2.718";

if (is_float($var1)) {
    echo "<span>$var1</span> is a floating‑point number";
} else {
    echo "<span>$var1</span> is not a floating‑point number";
}

if (is_float($var2)) {
    echo "<span>$var2</span> is a floating‑point number";
} else {
    echo "<span>$var2</span> is not a floating‑point number";
}

if (is_float($var3)) {
    echo "<span>$var3</span> is a floating‑point number";
} else {
    echo "<span>$var3</span> is not a floating‑point number";
}

Output:

3.14 is a floating‑point number
5 is not a floating‑point number
2.718 is a floating‑point number

Example 2: Combine with conditional statements

$price = 19.99;

if (is_float($price)) {
    if ($price >= 10) {
        echo "Price is reasonable";
    } else {
        echo "Price is unreasonable";
    }
} else {
    echo "Price format is incorrect";
}

Output:

Price is reasonable

These examples show that using is_float() can conveniently determine whether a variable is a float, enabling appropriate logical handling.

4. Summary

The is_float() function is a very practical PHP function that allows you to easily check if a variable is a floating‑point number. Using it during numeric calculations and type checking can effectively prevent errors and logical issues.

Note: is_float() distinguishes between integers and floats; if the variable is an integer, the function returns false. To check whether a variable is numeric (including both integers and floats), use the is_numeric() function.

We hope this article helps you understand and use is_float(). For further questions, refer to the official PHP documentation or engage with the PHP development community.

Java learning materials

C language learning materials

Frontend learning materials

C++ learning materials

PHP learning materials

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

type checkingfloating-pointis_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

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.