PHP 8.1.6 Released!


(PHP 4, PHP 5, PHP 7, PHP 8)

is_numeric Informa se a variável é um número ou uma string numérica


is_numeric(mixed $var): bool

Verifica se a dada variável é numérica. Strings numéricas consistem de um sinal opcional, algum número de dígitos, opcional parte decimal e opcional parte exponencial. Assim +0123.45e6 é um válido valor numérico. Notação hexadecimal (0xFF) é permitida também mas somente sem sinal, decimal e parte exponencial.



A variável a ser avaliada.

Valor Retornado

Retorna true se var é um número ou uma string numérica, false caso contrário.

Veja Também

  • ctype_digit() - Verifica se os caracteres são numéricos
  • is_bool() - Verifica se a variável é um boleano
  • is_null() - Informa se a variável é null
  • is_float() - Informa se a variável é do tipo float
  • is_int() - Informa se a variável é do tipo inteiro
  • is_string() - Informa se a variável é do tipo string
  • is_object() - Informa se a variável é um objeto
  • is_array() - Verifica se a variável é um array

add a note add a note

User Contributed Notes 6 notes

sobolanx at gmail dot com
11 years ago
Note that the function accepts extremely big numbers and correctly evaluates them.

For example:

= is_numeric ('58635272821786587286382824657568871098287278276543219876543') ? true : false;
var_dump ($v);

The above script will output:


So this function is not intimidated by super-big numbers. I hope this helps someone.

PS: Also note that if you write is_numeric (45thg), this will generate a parse error (since the parameter is not enclosed between apostrophes or double quotes). Keep this in mind when you use this function.
tanguy_barsik at hotmail dot com
5 years ago
for strings, it return true only if float number has a dot

is_numeric( '42.1' )//true
is_numeric( '42,1' )//false
moskalyuk at gmail dot com
15 years ago
is_numeric fails on the hex values greater than LONG_MAX, so having a large hex value parsed through is_numeric would result in FALSE being returned even though the value is a valid hex number
ben at chico dot com
8 years ago
Apparently NAN (Not A Number) is a number for the sake of is_numeric().

echo "is ";
if (!
"not ";
"a number";

Outputs "is a number". So something that is NOT a number (by defintion) is a number...
kouber at saparev dot com
18 years ago
Note that this function is not appropriate to check if "is_numeric" for very long strings. In fact, everything passed to this function is converted to long and then to a double. Anything greater than approximately 1.8e308 is too large for a double, so it becomes infinity, i.e. FALSE. What that means is that, for each string with more than 308 characters, is_numeric() will return FALSE, even if all chars are digits.

However, this behaviour is platform-specific.

In such a case, it is suitable to use regular expressions:

function is_numeric_big($s=0) {
  return preg_match('/^-?\d+$/', $s);
Magnus Deininger, dma05 at web dot de
13 years ago
regarding the global vs. american numeral notations, it should be noted that at least in japanese, numbers aren't grouped with an extra symbol every three digits, but rather every four digits (for example 1,0000 instead of 10.000). also nadim's regexen are slightly suboptimal at one point having an unescaped '.' operator, and the whole thing could easily be combined into a single regex (speed and all).


= preg_match
('/^[+-]?'. // start marker and sign prefix
'(((([0-9]+)|([0-9]{1,4}(,[0-9]{3,4})+)))?(\\.[0-9])?([0-9]*)|'. // american
'((([0-9]+)|([0-9]{1,4}(\\.[0-9]{3,4})+)))?(,[0-9])?([0-9]*))'. // world
'(e[0-9]+)?'. // exponent
'$/', // end marker
$str) == 1;

i'm sure this still isn't optimal, but it should also cover japanese-style numerals and it fixed a couple of other issues with the other regexen. it also allows for an exponent suffix, the pre-decimal digits are optional and it enforces using either grouped or ungrouped integer parts. should be easier to trim to your liking too.
To Top