is_numeric
Checks whether a value is a number or a numeric string that PHP can safely use as one.
is_numeric(mixed $value): bool- Added in PHP 4.0
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The variable being evaluated. |
Return value
booltrue if value is an int, a float, or a numeric string; false otherwise.
Changelog
- PHP 8.0.0Numeric strings with trailing whitespace, such as "42 ", now return true. Previously they returned false.
is_numeric() accepts integers and floats as-is, and accepts strings that PHP considers numeric strings: optional leading whitespace, an optional sign, decimal digits, an optional decimal point, and optional exponent notation. "42", "-3.14", "1e5", ".5" and "0b0"-free forms all pass.
When to reach for it
Use it on values coming from $_GET, $_POST, CSV rows or JSON, where everything arrives as a string and you want to know whether arithmetic will be meaningful. It is the correct guard before (int) or (float) casts.
What trips people up
Hexadecimal strings are not numeric. is_numeric("0x1A") has returned false since PHP 7. Binary ("0b11") and octal-with-prefix ("0o17") strings fail too — only the literal forms in source code understand those prefixes.
Leading whitespace has always been allowed; trailing whitespace only became acceptable in PHP 8.0, so "42 " returns true on modern PHP and false on PHP 7.
It is also not an integer check. "3.9", "1e3" and "-0" are all numeric. If you need digits only — an ID, a postcode, a phone number where leading zeros matter — use ctype_digit() instead, and remember that ctype_digit() only makes sense on strings.
Example
When to use it
- Validating a quantity or price field submitted from a form before casting it
- Deciding whether a CSV column should be summed or treated as a label
- Filtering a mixed array down to values you can safely pass to array_sum()
Frequently asked questions
Does is_numeric accept decimals and negative numbers?
Yes. Floats, decimal strings like "3.14", negative signs and exponent notation such as "1e5" all return true. It is not restricted to whole numbers.
Why does is_numeric return false for "0x1A"?
Hexadecimal strings stopped being treated as numeric in PHP 7. Only integer, float and exponent-notation strings qualify; use hexdec() or intval($s, 16) to parse hex text.
What is the difference between is_numeric and ctype_digit?
is_numeric() accepts anything PHP can use as a number, including signs, decimal points and exponents. ctype_digit() returns true only when every character is 0-9, which is what you want for IDs and zero-padded codes.
More examples
Validating form input before casting
Adding 0 to a validated numeric string promotes it to int or float automatically — '3' becomes int, '19.99' becomes float. The hex-looking ref is rejected because PHP 7 stopped treating hex strings as numeric.
<?php
$input = ['qty' => '3', 'price' => '19.99', 'code' => 'A12', 'ref' => '0x1A'];
foreach ($input as $field => $raw) {
if (!is_numeric($raw)) {
echo "$field: rejected ('$raw' is not a number)\n";
continue;
}
$n = $raw + 0;
echo "$field: ", get_debug_type($n), " ", $n, "\n";
}is_numeric versus ctype_digit
ctype_digit() is stricter: it rejects signs, decimal points, exponents and whitespace, but accepts '007' where leading zeros must be preserved. Use it for identifier-style fields and is_numeric() for anything you intend to do arithmetic on.
<?php
$tests = ['007', '42', '3.14', '-5', '1e3', ' 8'];
foreach ($tests as $t) {
printf("%-6s is_numeric:%-6s ctype_digit:%s\n",
"'$t'",
is_numeric($t) ? 'yes' : 'no',
ctype_digit($t) ? 'yes' : 'no'
);
}
Discussion