Master PHP’s is_numeric(): How to Validate Numbers with Code Examples

Learn how PHP’s is_numeric() function checks whether a variable is numeric, see detailed explanations, multiple code snippets for direct values and form inputs, and understand special cases like decimal points and signs to reliably validate numbers in your applications.

php Courses
php Courses
php Courses
Master PHP’s is_numeric(): How to Validate Numbers with Code Examples

In PHP programming, you often need to determine whether a variable is numeric. To solve this, PHP provides a convenient function— is_numeric(). The is_numeric() function 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 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 the input is a valid number. Example code:

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

In this example, $_POST['number'] is the user‑entered value; is_numeric() determines if it is numeric and outputs the appropriate message.

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 you to easily determine if a value is a valid number, though you should be aware of special case handling.

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.

PHPphp-functionsis_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

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.