简体   繁体   English

如何检查数字值

[英]How to check a Value for number

how can i check in PHP a value for a number? 如何检查PHP中的数字值? What kind is better? 哪种更好? Check ctype_digit() for a string automatically? 自动检查ctype_digit()是否有字符串?

<?php 
if(ctype_digit($value)){
echo 'Yes';
} else {
echo 'No';
}

if(is_string($value) && ctype_digit($value)){
echo 'Yes';
} else {
echo 'No';
}
?>

要检查变量是数字还是数字字符串,请使用is_numeric函数:

echo (is_numeric($value))? 'yes' : 'no';

You can use the gettype() method which returns the variable type as a string, or you can just use is_string(), is_int(), is_float(), is_object(), is_array(), etc. to evaluate if the variable is of a given type. 您可以使用gettype()方法将变量类型返回为字符串,也可以只使用is_string(),is_int(),is_float(),is_object(),is_array()等来评估变量是否为给定类型的

You can see the full documentation here: http://php.net/manual/en/function.gettype.php 您可以在此处查看完整的文档: http : //php.net/manual/en/function.gettype.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM