Backend Development 4 min read

Using PHP's is_numeric() Function to Check Numeric Values

This article explains PHP's is_numeric() function, detailing its purpose, usage, return values, and special cases, and provides multiple code examples—including basic numeric checks, form input validation, and edge-case behaviors—to help beginner developers correctly determine whether variables are numeric.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP's is_numeric() Function to Check Numeric Values

In PHP programming, it is often necessary to determine whether a variable is numeric. To solve this, PHP provides a convenient function— is_numeric() —which checks if a variable is numeric and returns a boolean true or false. This article details the is_numeric() function and provides code examples.

The is_numeric() function can detect whether a variable is numeric. It accepts one argument, the variable to check, which can be an integer, a float, or even a numeric string. If the variable is numeric, it returns true; otherwise, it returns false.

Below is a code example using the is_numeric() function:

$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 the above example, variables $var1 , $var2 , and $var3 are numeric, so is_numeric() returns true. Variable $var4 is a string, not numeric, so the function returns false.

The is_numeric() function can also be used to validate form input. For example, when a user submits a form, you can use is_numeric() to verify that the input is a valid number. Example code:

if(is_numeric($_POST['number'])) {
    echo "输入的是一个数值";
} else {
    echo "输入的不是一个数值";
}

In this example, $_POST['number'] is the user‑submitted value; is_numeric() checks it. If it is numeric, the script outputs “输入的是一个数值” (the input is a number); otherwise, it outputs “输入的不是一个数值” (the input is not a number).

Note that is_numeric() may not handle some special cases ideally. For instance, a plus or minus sign and a trailing decimal point are considered non‑numeric. is_numeric("12.34") returns true, but is_numeric("12.") returns false.

In summary, the is_numeric() function is a very useful PHP function for checking whether a variable is numeric. It allows convenient validation of legitimate numeric values, though special cases require careful handling.

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

PHP Practice

Scan QR code to receive free learning materials

backend developmentPHPform validationis_numericnumeric validation
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.