PHP is_numeric() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Variables

Definition

In PHP, is_numeric() checks whether a value is a number or a numeric string. It returns true for integers, floats and strings such as '42', '3.14' and '1e3'.

This function is very handy for form input, because everything a user types arrives as a string. is_numeric() tells you whether that string can safely be used as a number. Text such as 'abc' or '12abc' and an empty string return false.

Syntax

Call is_numeric() with the values you need. Start with this line.

PHP
$ok = is_numeric($input);
CallWhat it does
is_numeric('42')Returns true because the string holds a whole number.
is_numeric('3.14')Returns true because the string holds a decimal number.
is_numeric('12abc')Returns false because the string ends with letters.

Since PHP 8.0, leading and trailing spaces are allowed, so ' 5' and '5 ' are numeric. Hex strings like '0x1A' and the empty string are not numeric.

Library declarations
PHP
is_numeric(mixed $value): bool

Parameters

ParameterDescription
valueRequired. The value to check. Numbers always return true, and strings return true only when they form a valid number.

Return Value

Return ValueDescription
boolReturns true if the value is a number or a numeric string, and false otherwise.

PHP Examples

Example 1 — Numbers and Numeric Strings

Check an integer, a numeric string, a decimal string and plain text.

PHP
<?php
var_dump(is_numeric(42));
var_dump(is_numeric('42'));
var_dump(is_numeric('3.14'));
var_dump(is_numeric('abc'));

How It Works

  1. 42 is already a number. Real integers always return true.
  2. '42' is a numeric string. It contains only digits, so PHP accepts it as a number.
  3. '3. 14' is a decimal string. A single decimal point is allowed, so the result is true.
  4. 'abc' has no digits. It cannot be read as a number, so the result is false.

Example 2 — Tricky Strings

Some strings look odd but are valid numbers, while others look fine and are not. Try a few of them.

PHP
<?php
var_dump(is_numeric('1e3'));
var_dump(is_numeric(' 5'));
var_dump(is_numeric('5 '));
var_dump(is_numeric('0x1A'));
var_dump(is_numeric(''));
var_dump(is_numeric(null));

How It Works

  1. '1e3' uses scientific notation. It means 1000, so it is numeric.
  2. Spaces at the start and end are allowed. Trailing spaces are accepted from PHP 8.0 onward.
  3. '0x1A' is a hex string. PHP no longer treats hex strings as numeric, so it returns false.
  4. An empty string and null are not numbers. Both of the last two results are false.

Example 3 — Validating User Input

Loop over some typed values, double the valid numbers and reject everything else.

PHP
<?php
$inputs = ['25', '2.5', 'ten', '12abc'];

foreach ($inputs as $input) {
    if (is_numeric($input)) {
        echo $input, ' is a number, doubled: ', $input * 2, PHP_EOL;
    } else {
        echo $input, ' is not a number', PHP_EOL;
    }
}

How It Works

  1. The array simulates form input. Every value is a string, just like data from a form.
  2. is_numeric() decides for each value. Only '25' and '2.5' pass the check.
  3. Valid values are multiplied by 2. PHP converts the numeric string to a number automatically.
  4. Invalid values are rejected. 'ten' and '12abc' are skipped before any math is done.

Frequently Asked Questions

It checks whether a value is a number or a string that can be read as a number. It returns true for integers, floats and strings like '42' or '1.5', and false for other text.
Yes. Scientific notation is a valid number in PHP, so '1e3' returns true. If you only want plain digits, use ctype_digit() instead.
No. is_numeric('') returns false. null also returns false, so you do not need a separate check for those two values.
is_int() only returns true for real integers. is_numeric() also accepts floats and numeric strings, so is_int('5') is false but is_numeric('5') is true.
In PHP 8.0 and later, leading and trailing whitespace is allowed, so ' 5' and '5 ' are numeric. Spaces in the middle, such as '5 5', are not.

Next: is_object()

is_object() checks whether a value is an object. It returns true for instances of any clas.

is_object() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful