Check if Variable is Number in PHP

In PHP, you can use the following method to determine if a string is a number:

  1. Utilize the is_numeric() function:
$num = "123";
if (is_numeric($num)) {
    echo "是数字";
} else {
    echo "不是数字";
}
  1. Utilize the is_int() function (only for integers):
$num = "123";
if (is_int($num)) {
    echo "是数字";
} else {
    echo "不是数字";
}
  1. Use the ctype_digit() function (only for positive integers):
$num = "123";
if (ctype_digit($num)) {
    echo "是数字";
} else {
    echo "不是数字";
}

No matter how you do it, if the string being evaluated can be successfully converted to a number, it will return true, otherwise it will return false.

bannerAds