Using PHP is_numeric() to Determine If a Variable Is Numeric

This article explains PHP's is_numeric() function, detailing how it determines whether a variable is numeric, provides multiple code examples—including basic checks, form input validation, and edge cases—while highlighting special considerations such as handling of decimal points and signs.

php Courses
php Courses
php Courses
Using PHP is_numeric() to Determine If a Variable Is Numeric

In PHP programming, it is often necessary to determine whether a variable holds a numeric value. PHP provides a convenient function, is_numeric(), which checks a variable and returns a boolean true or false.

The is_numeric() function accepts a single argument—the variable to be examined—and returns true if the variable is an integer, a float, or a numeric string; otherwise it returns false.

Below is a code example demonstrating the use of is_numeric():

$var1 = 123;
$var2 = 3.14;
$var3 = "42";
$var4 = "abc";

echo is_numeric($var1);  // outputs 1
echo is_numeric($var2);  // outputs 1
echo is_numeric($var3);  // outputs 1
echo is_numeric($var4);  // outputs empty string

In this example, $var1, $var2, and $var3 are considered numeric, so is_numeric() returns true for them, while $var4 is a non‑numeric string, resulting in false.

The function can also be used to validate form input. For instance, when a user submits a form, you can verify that the submitted value is numeric before processing it:

if (is_numeric($_POST['number'])) {
    echo "输入的是一个数值"; // "The input is a numeric value"
} else {
    echo "输入的不是一个数值"; // "The input is not a numeric value"
}

Here, $_POST['number'] is checked with is_numeric(); the script outputs a corresponding message based on whether the input is numeric.

Note that is_numeric() has some edge‑case behavior. Characters such as a leading plus/minus sign or a trailing decimal point are treated specially. For example, is_numeric("12.34") returns true, but is_numeric("12.") returns false.

In summary, is_numeric() is a useful PHP function for checking if a variable is numeric, enabling developers to easily validate data and handle numeric values appropriately, while being aware of its handling of special cases.

Hopefully this introduction helps beginner PHP developers better understand and use the is_numeric() function.

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.

validationis_numericnumeric check
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.