Definition and Usage
- PHP Version
- 4+
The is_numeric() function checks whether a given value is a number or a numeric string.
It returns true if the value is numeric, and false otherwise.
Number
Refers to both the integer type, which contains whole numbers without decimal points, and the floating-point type, which includes numbers with decimal points.
Numeric String
In PHP, this includes strings that can be interpreted as integers or floating-point numbers, meaning any string that represents a numeric value.
Basic Example
/* Integer Type */
var_dump(is_numeric(1)); // bool(true)
var_dump(is_numeric(0)); // bool(true)
var_dump(is_numeric(-10)); // bool(true)
/* Floating-Point Numbers (float) */
var_dump(is_numeric(1.0)); // bool(true)
var_dump(is_numeric(0.0)); // bool(true)
var_dump(is_numeric(3.14)); // bool(true)
/* Numeric Strings */
var_dump(is_numeric('1')); // bool(true)
var_dump(is_numeric('0')); // bool(true)
var_dump(is_numeric('10.5')); // bool(true)
var_dump(is_numeric('123e2')); // bool(true)
/* Non-Numeric Strings */
var_dump(is_numeric('hello')); // bool(false)
var_dump(is_numeric('abc123')); // bool(false)
/* Results of Numeric Operations */
var_dump(is_numeric(1 + 1)); // bool(true)
var_dump(is_numeric(10 / 2)); // bool(true)
var_dump(is_numeric(9 / 2)); // bool(true)
/* Type Casting */
var_dump(is_numeric((float) '1')); // bool(true)
var_dump(is_numeric((int) '10.5')); // bool(true)
var_dump(is_numeric((string) 123)); // bool(true)
Note:
The gettype() function returns the data type of the given value as a string.
Syntax
is_numeric(mixed $value): bool
Parameters
$value |
Required. The value to check whether it is a number or a numeric string. |
|---|
Return Values
Returns true if the given value is a number or a numeric string, and false otherwise.
Changes as of PHP 8.0.0
- Prior to PHP 8.0.0, numeric strings with trailing whitespace were
not recognized as numbersby theis_numeric()function. For example,'35 'would returnfalsebecause it contained a trailing space. - Starting with PHP 8.0.0,
is_numeric()now returnstruefor such strings.
var_dump(is_numeric('35 ')); // bool(false) => numeric string with trailing whitespace
var_dump(is_numeric(' 35')); // bool(true) => numeric string with leading whitespace
var_dump(is_numeric('35 ')); // bool(true) => returns true starting from PHP 8.0.0
var_dump(is_numeric(' 35')); // bool(true) => still returns true for strings with leading whitespace
In short, this change allows is_numeric() to consider numeric strings with trailing whitespace as valid numbers, returning true.
Things to Keep in Mind
Strings that include a + or - sign are recognized as numbers by the is_numeric() function. For example, '+123' or '-456' are considered numeric.
var_dump(is_numeric('+123')); // bool(true)
var_dump(is_numeric('-456')); // bool(true)
To check if a string contains only digits, use the ctype_digit() function.
ctype_digit()
var_dump(ctype_digit('+123')); // bool(false)
var_dump(ctype_digit('-456')); // bool(false)
var_dump(ctype_digit('456')); // bool(true)
Code Explanation
The ctype_digit() function checks whether a given string consists entirely of digits (0–9).
It returns true if all characters are digits, and false otherwise.
References
See also
- PHP gettype() Function – Get the Data Type of a Variable as a String
- PHP is_bool() Function – Checking the Boolean Type of a Variable
- PHP is_int() Function – Checking the Integer Type of a Variable
- PHP is_float() Function – Checking the Float Type of a Variable
- PHP is_string() Function – Checking the String Type of a Variable
- PHP isset() and empty() Functions – Concepts, Usage, and Key Differences
- PHP Superglobals – Simple and Essential Usage Guide