What is the is_numeric method in PHP?

The is_numeric method can be used to check if the value is a numeric value or not. The value also includes a numeric string.

Syntax

is_nueric(mixed $value): bool

This method returns true if the passed value is a numeric value or a numeric string. Otherwise, it returns false.

Code

<?php
$test = 123;
printIsNumeric($test);
$test = "123";
printIsNumeric($test);
$test = "1337e0";
printIsNumeric($test);
$test = "1 2";
printIsNumeric($test);
function printIsNumeric($test){
echo "The Value is: ";
var_dump($test);
echo "Is Numeric: ";
var_dump(is_numeric($test));
echo "---------\n\n";
}
?>

Explanation

In the above code:

  • We used the is_numeric method to check if a value is a numeric value.

  • For the value 123(int), "123"(string), and 1337e0(string), the is_numeric method returns true.

  • But for the value 1 2(string), the is_numeric method returns false because it is not a numeric value (it has whitespace between 1 and 2).

Free Resources