2011-11-30 36 views

回答

2

你可以使用函数: is_numeric()来 - 查找无论变量是数字还是数字串..

例如:

<?php 
$tests = array(
    "42", 
    1337, 
    "1e4", 
    "not numeric", 
    array(), 
    9.1 
); 

foreach ($tests as $element) { 
    if (is_numeric($element)) { 
     echo "'{$element}' is numeric", PHP_EOL; 
    } else { 
     echo "'{$element}' is NOT numeric", PHP_EOL; 
    } 
} 
?> 

上面的例子将输出:

'42' is numeric 
'1337' is numeric 
'1e4' is numeric 
'not numeric' is NOT numeric 
'Array' is NOT numeric 
'9.1' is numeric 

来源:http://php.net/manual/en/function.is-numeric.php

0

如果你想知道的是,如果它是一个数字,那么PHP提供了一个调用的函数is_numeric,如果它是数字,则返回true。

但是,如果您想知道某物是否为整数,请不要使用is_int。使用filter_var($variable, FILTER_VALIDATE_INT)

如果你想知道某物是浮点数(例如1.00123):filter_var($variable, FILTER_VALIDATE_FLOAT)

相关问题