In PHP, it is often necessary to judge and verify the data type of variables. Among them, determining whether a variable is of numeric type is a very common operation. PHP provides a built-in function is_numeric() to check whether a variable is of numeric type.
The usage format of is_numeric() function is as follows:
bool is_numeric (mixed $var)
Among them, $var is the variable that needs to be checked, it can be any PHP data Type, including string, integer, floating point, etc. When $var is a number or numeric string, the is_numeric() function returns true, otherwise it returns false.
Below we use specific code examples to illustrate the use of the is_numeric() function.
Sample code:
<?php $num1 = 123; $num2 = "456"; $num3 = "789hello"; if (is_numeric($num1)) echo '$num1是数字'; if (is_numeric($num2)) echo '$num2是数字'; if (is_numeric($num3)) echo '$num3是数字'; ?>
Output result:
$num1是数字 $num2是数字
Analysis:
We have defined three variables $num1, $num2 and $num3, where $num1 is an integer and $num2 and $num3 are numeric strings. By judging these three variables through the is_numeric() function, you can see that $num1 and $num2 are both recognized as numbers, and $num3 is judged as a non-numeric variable because it contains the non-numeric character "hello".
Sample code:
<?php $num1 = 12.34; $num2 = "56.78"; $num3 = "90.12hello"; if (is_numeric($num1)) echo '$num1是数字'; if (is_numeric($num2)) echo '$num2是数字'; if (is_numeric($num3)) echo '$num3是数字'; ?>
Output result:
$num1是数字 $num2是数字
Analysis:
This time we defined three floating-point numeric variables $num1, $num2 and $num3, where $num1 and $num2 are floating-point numbers, and $num3 contains non-numeric characters. By judging these three variables through the is_numeric() function, you can see that $num1 and $num2 are both recognized as numbers, while $num3 is judged as a non-numeric variable.
Sample code:
<?php $str1 = "Hello World"; $str2 = "1234"; $bool1 = true; $arr1 = array('a', 'b', 'c'); if (is_numeric($str1)) echo '$str1是数字'; if (is_numeric($str2)) echo '$str2是数字'; if (is_numeric($bool1)) echo '$bool1是数字'; if (is_numeric($arr1)) echo '$arr1是数字'; ?>
Output result:
$str2是数字
Parsing:
This time we defined four different data type variables $str1, $str2, $bool1 and $arr1. $str1 is a string type, $str2 is a numeric string type, $bool1 is a Boolean type, and $arr1 is an array type. Through the is_numeric() function to judge these four variables, you can see that only $str2 is judged to be a numeric type, and the other three variables are not numeric types.
Through these specific code examples, we can see how to use the is_numeric() function and the judgment rules. In actual PHP development, in many cases it is necessary to judge and verify the data type of variables. The is_numeric() function is a very convenient built-in function that can help us quickly judge the numeric type of variables.
The above is the detailed content of is_numeric() function in PHP: How to check if a variable is numeric. For more information, please follow other related articles on the PHP Chinese website!