PHP is_numeric() function

Description

Finds whether a variable is a number or a numeric string.

Syntax

PHP is_numeric() function has the following syntax.

bool is_numeric ( mixed $var )

Parameter

  • var - The variable being evaluated.

Return

Returns TRUE if var is a number or a numeric string, FALSE otherwise.

Example

Is it a numeric value?


<?php/*ww w  .j  a  v a 2 s.co m*/
$tests = array(
    "4",
    12,
    0x29,
    0124,
    0b111001,
    137e0,
    "not numeric",
    array(),
    9.1
);

foreach ($tests as $element) {
    if (is_numeric($element)) {
        echo "'{$element}' is numeric", PHP_EOL;
    } else {
        echo "'{$element}' is NOT numeric", PHP_EOL;
    }
}
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions