php预定义常量规则_PHP: 预定义常量 - Manual

Caution when using variables with the value NAN (or directly NAN, which would not be logical) if the value is converted in any form (integer, string or other non-float format)!

In that case, NAN is converted to 0. (e.g. as an index in an array, argument of a method that takes it as a non-float parameter, etc.). This bug has been reported several times, but is still present in the current version PHP 7.3.15.

Example 1:

$array = [0 =>  'zero', 1 => 'one', 2 => 'two'];

$index = NAN;

echo $array[$index]; // echo 'zero' and not as axcepted throws an exception or at least a warning 'undefined offset NAN ...'

Example 2:

function f((int) $p) { return $p; }

echo f(NAN); // echo 0 because the argument (NAN) has been converted to integer (0)

It would have to be checked in both cases explicitly for is_nan():

1: if(is_nan($index)){

}else{

echo $array[$index];

};

2: function f($p){ // without converting!!!

if(is_nan($p)){

}else{

return (int)$p; // actually a correct conversion  to Integer would have to be checked because the parser does not check this here.

};

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值