is_numeric()

is_numeric() — Finds whether a variable is a number or a numeric string.

Built-in function of PHP 8.5. The manual lists it as available in PHP 4, PHP 5, PHP 7, PHP 8. This page is the reference card: signature, parameters and return value, plus the changelog and the example the manual provides.

Signature

is_numeric(mixed $value): bool

Parameters

ParameterTypeWhat it takes
$valuemixedThe variable being evaluated.

Return value

Returns true if value is a number or a numeric string, false otherwise.

Example

A runnable snippet:

$tests = array( "42", 1337, 0x539, 02471, 0b10100111001, 1337e0, "0x539", "02471", "0b10100111001", "1337e0", "not numeric", array(), 9.1, null, '', ); foreach ($tests as $element) { if (is_numeric($element)) { echo var_export($element, true) . " is numeric", PHP_EOL; } else { echo var_export($element, true) . " is NOT numeric", PHP_EOL; } }

Output:

'42' is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337 is numeric 1337.0 is numeric '0x539' is NOT numeric '02471' is numeric '0b10100111001' is NOT numeric '1337e0' is numeric 'not numeric' is NOT numeric array ( ) is NOT numeric 9.1 is numeric NULL is NOT numeric '' is NOT numeric

Changelog

VersionChange
8.0.0Numeric strings ending with whitespace ("42 ") will now return true. Previously, false was returned instead.

See also

ctype_digit(), is_bool(), is_null(), is_float(), is_int(), is_string(), is_object(), is_array(), filter_var()

All PHP 8.5 built-in functions

Reference card built from the PHP manual page is_numeric() (PHP documentation, CC BY 3.0). The page is generated from the manual, so the wording of the parameter and return value text follows php.net.