函数来检查值是int还是float而不是PHP中的字符串
Is there any build-in function to check the value is number, in any integral type, like int, float but not string. is_numeric return true for "2". should I check it by both is_int and is_float?
The function that I want should return "invalid value" when $a and $b are not number:
function myFunction($a, $b, $sign){}
是否有任何内置函数来检查值是否为数字,在任何整数类型中,如int,float但是 not string。 is_numeric为“2”返回true。 我应该通过is_int和is_float检查它吗? p>
当$ a和$ b不是数字时,我想要的函数应返回“无效值”: p>
function myFunction($ a,$ b,$ sign){}
code> pre>
div>
is_numeric($value) && !is_string($value)
this is what I was looking for
This isn't an answer per se, but this is too much to put in a comment:
function isFloatOrInt($var)
{
$type = gettype($var);
switch ($type) {
case 'integer':
case 'double':
return true;
case 'string':
//check for number embedded in strings "1" "0.12"
return preg_match('/^[0-9]+(?:\.[0-9]+)?$/', $var);
default:
return false;
}
Preg match can be useful here if you consider the string version of a number to still be a number.
You say
But not string
But does that mean this 'string'
or this '10'
instead of 10
for example, ie. a string that is also a number?
There is probably many ways to do this, but there are some edge cases so it's largely depends on your needs. For example is an octet
a number to you 0777
or and exponent 12e4
or even Hexadecimal 0xf4c3b00c
?