PHP Function Reference

PHP is_numeric() Function



The PHP is_numeric() function checks whether a variable is a number or a numeric string. The function returns true if the variable is a number or a numeric string, otherwise it returns false.

Syntax

is_numeric(variable)

Parameters

variable Required. Specify the variable being evaluated.

Return Value

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

Example:

The example below shows the usage of is_numeric() function.

<?php
var_dump(is_numeric(10));     //returns: bool(true)
var_dump(is_numeric(10.5));   //returns: bool(true)
var_dump(is_numeric(1e5));    //returns: bool(true)
var_dump(is_numeric('10.5')); //returns: bool(true)
var_dump(is_numeric('10'));   //returns: bool(true)
var_dump(is_numeric('1e5'));  //returns: bool(true)

echo "\n";

var_dump(is_numeric('xyz'));   //returns: bool(false)
var_dump(is_numeric(true));    //returns: bool(false)
var_dump(is_numeric("true"));  //returns: bool(false)
?>

The output of the above code will be:

bool(true)
bool(true)
bool(true)
bool(true)
bool(true)
bool(true)

bool(false)
bool(false)
bool(false)

Example:

Consider one more example where this function is used to check all elements of an array if they are either a number or a numeric string.

<?php
$Arr = array(10, "10", 10.5, "10.5", null, false, 
             "true", 1e3, "1e3", "", 0x539, 0257, 
             0b10100111001, "0x539", "0257", 
             "0b10100111001", array()); 

foreach ($Arr as $value) {
  echo "is_numeric(".var_export($value, true).") = ";
  var_dump(is_numeric($value));
}
?>

The output of the above code will be:

is_numeric(10) = bool(true)
is_numeric('10') = bool(true)
is_numeric(10.5) = bool(true)
is_numeric('10.5') = bool(true)
is_numeric(NULL) = bool(false)
is_numeric(false) = bool(false)
is_numeric('true') = bool(false)
is_numeric(1000.0) = bool(true)
is_numeric('1e3') = bool(true)
is_numeric('') = bool(false)
is_numeric(1337) = bool(true)
is_numeric(175) = bool(true)
is_numeric(1337) = bool(true)
is_numeric('0x539') = bool(false)
is_numeric('0257') = bool(true)
is_numeric('0b10100111001') = bool(false)
is_numeric(array (
)) = bool(false)

❮ PHP Variable Handling Reference