php 检查php变量是否为数字

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9761907/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:35:32  来源:igfitidea点击:

Checking if php variable is a number or not

php

提问by CJS

Possible Duplicate:
What's the correct way to test if a variable is a number in PHP?

可能的重复:
在 PHP 中测试变量是否为数字的正确方法是什么?

I have two variables coming in from a form:

我有两个来自表单的变量:

$minNo = mysql_real_escape_string($_GET["minNo"]);
$maxNo = mysql_real_escape_string($_GET["maxNo"]);

How can I check whether they are numerical values or not?

如何检查它们是否是数值?

回答by j08691

Try is_numeric.

试试is_numeric

Finds whether the given variable is numeric. Numeric strings consist of optional sign, any number of digits, optional decimal part and optional exponential part. Thus +0123.45e6 is a valid numeric value. Hexadecimal notation (0xFF) is allowed too but only without sign, decimal and exponential part.

查找给定变量是否为数字。数字字符串由可选的符号、任意数量的数字、可选的小数部分和可选的指数部分组成。因此 +0123.45e6 是一个有效的数值。也允许使用十六进制表示法 (0xFF),但不能使用符号、十进制和指数部分。

回答by MarcinJuraszek

Use is_numericfunction, it returns bool value:

使用is_numeric函数,它返回布尔值:

is_numeric($element)

回答by alex

You can use is_numeric()to check if the number is numeric.

您可以使用is_numeric()检查数字是否为数字。

To validate as an integer, use...

要验证为整数,请使用...

$validInt = filter_var($minNo, FILTER_VALIDATE_INT);

回答by Cristian Rodriguez

using is_numeric() function...

使用 is_numeric() 函数...