Backend Development 4 min read

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

This article explains the PHP is_numeric() function, shows how it checks whether a variable (including integers, floats, or numeric strings) is numeric, provides code examples for direct usage and form validation, and notes special cases to watch out for.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Using PHP is_numeric() to Determine if a Variable Is Numeric

In PHP programming, you often need to determine whether a variable is numeric. PHP provides the convenient function is_numeric() to check this and return a boolean true or false.

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

Example usage 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 numeric, so is_numeric() returns true, while $var4 is a non‑numeric string, so it returns false.

The function can also be used to validate form input. For instance, you can check $_POST['number'] with is_numeric() and output a message accordingly:

if (is_numeric($_POST['number'])) {
    echo "Input is a number";
} else {
    echo "Input is not a number";
}

Note that is_numeric() may not handle some edge cases as expected; for example, a trailing decimal point like "12." is considered non‑numeric.

In summary, is_numeric() is a useful PHP function for checking whether a variable is numeric, helping you validate and process data correctly, though you should be aware of its limitations with certain formats.

This guide aims to help beginner PHP developers understand and use is_numeric() effectively.

backend developmentPHPCode Exampleis_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.