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_numericmethod to check if a value is a numeric value. -
For the value
123(int),"123"(string), and1337e0(string), theis_numericmethod returnstrue. -
But for the value
1 2(string), theis_numericmethod returnsfalsebecause it is not a numeric value (it has whitespace between 1 and 2).