In this PHP tutorial, you shall learn how to check if given string is a number using is_numeric() function, with example programs.

PHP – Check if String is Number

To check if given string is a number or not, use PHP built-in function is_numeric(). is_numeric() takes string as an argument and returns true if the string is all numbers, else it returns false.

The syntax of is_numeric() to check if the given string is a number or not is

is_numeric( $string )

Examples

ADVERTISEMENT

1. Check if given string is a number

In this example, we will take a string that has digits for all of its characters. We shall then pass this string as argument to is_numeric() function. The function should return true, since the string is all numeric digits.

PHP Program

<?php
$string = "4521";
if ( is_numeric($string) ) {
    echo "\"{$string}\" is a number.";
} else {
    echo "\"{$string}\" is not a number.";
}
?>

Output

PHP - Check if String is Number

Conclusion

In this PHP Tutorial, we learned how to check if given string is a number or not using PHP built-in function is_numeric().