PHP Tutorial - PHP is_numeric() function






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/*  w  ww. j  a  v a2 s. com*/
$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.