php method to determine whether a variable is a number: 1. Use the "gettype($var)" statement. If the return value is "integer" or "double", it is a number; 2. Use "is_numeric($var) )" statement, if the return value is "TRUE" it is a number and a numeric string.
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
php judgment variable is Not a number
Method 1: Use the gettype() function
<?php header("Content-type:text/html;charset=utf-8"); $num=123.25; if(gettype($num)=="integer"||gettype($num)=="double"){ echo "是数字"; }else{ echo "不是数字"; } ?>
Output result:
是数字
Instructions: The gettype() function is used to get the type of a variable. The return value can be:
boolean
integer
##double (Since PHP 4, if it is a float, it will return "double" instead of "float")
#string
array
object
resource (resource)
NULL
unknown type
Detect whether the variable $var is a number or a numeric string. If $var is, return TRUE, otherwise return FALSE; syntax "
is_numeric($var)". <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"><?php
header("Content-type:text/html;charset=utf-8");
$num=123;
if(is_numeric($num)){
echo "是数字";
}else{
echo "不是数字";
}
?></pre><div class="contentsignin">Copy after login</div></div>
Output result:
是数字
The above is the detailed content of How to determine whether a variable is a number in php. For more information, please follow other related articles on the PHP Chinese website!