How does php determine whether a variable is a number?
This article will explain in detail how php determines whether a variable is a number or not. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
Php to determine whether the variable is a number: 1, use the "gettype ($var)" statement, if the return value is "integer" or "double", it is a number; 2, use the "is_numeric ($var)" statement, if the return value is "TRUE", it is a number and a numeric string.
Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
Php determines whether a variable is a number or not
Method 1: use the gettype () function
Output result:
It's numbers.
Note: the gettype () function is used to obtain the type of variable, and the return value can be:
Boolean
Integer
Double (from PHP 4, return "double" instead of "float" if it is float)
String
Array
Object
Resource (Resource)
Resource is a special type of variable that holds a reference to an external resource; it mainly describes an extended resource of PHP. Resource (resources) are created and used through special functions.
NULL
NULL is also a special data type, which has only one value, NULL, which represents a null value (that is, a variable has no value).
Unknown type
Method 2: use the is_numeric () function
Checks whether the variable $var is a number or a numeric string, and returns TRUE if $var is, otherwise the FALSE; syntax "is_numeric ($var)" is returned.
Output result:
It's numbers.
About php how to judge whether the variable is a number or not, I hope the above content can be of some help to everyone and can learn more knowledge. If you think the article is good, you can share it for more people to see.