php is num,PHP – 必须使用is_numeric(),还是可以使用比较符号来处理所有正数字情况?...

似乎简单的比较符号>,> =并且它们的反向组件可以评估某个变量是否是数字.示例$whatami =’beast’; ($whatami< 0)?echo'NaN':echo'是数字!';

是否存在正值(数字> 0)需要使用is_numeric()的情况?似乎使用上面的比较符号将确定变量是否为数字.

解决方法:

正如我已经发现的那样,很多这些辅助函数确实是必要的,因为PHP不是强类型的.本周早些时候我发布了一个similar question(尽管不是那么相似).需要注意的一点是,在某些情况下(当存在混合类型时),PHP会将您的字符串更改为其整数值以进行比较.这不容忽视.我认为这是is_numeric的一个强有力的案例

If you compare a number with a string

or the comparison involves numerical

strings, then each string is converted

to a number and the comparison

performed numerically. These rules

also apply to the switch statement.

The type conversion does not take

place when the comparison is === or

!== as this involves comparing the

type as well as the value.

要考虑的另一件事是PHP中的“0是什么”.那意义重大.它并不总是数字.它可能是数字字符串,布尔值false,整数等…这就是存在这些辅助函数的原因.

要添加到我的答案:

改变你的榜样:

$whatami='beast';

($whatami<5) ? echo 'less than 5' : echo 'more than 5';

PHP会将’beast’更改为等效的整数,然后进行比较.这会产生意想不到的结果.如果你真的想要类似的东西,你必须用另一个条件包装它:

$whatami='beauty';

if(is_numeric($whatami){

($whatami<5) ? echo 'less than 5' : echo 'more than 5';

} else {

exit('what, am I not pretty enough for a beast?');

}

然后你会得到你想要的结果(尽可能奇怪).

标签:isnumeric,evaluation,php,math,numeric

来源: https://codeday.me/bug/20190730/1581911.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值