note 124871 added to math.constants
| From: | Kira-Bianca dot Hinz at web dot de | Date: | Wed, 01 Apr 2020 16:51:36 +0000 |
| Subject: | note 124871 added to math.constants | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-214401@lists.php.net to get a copy of this message | ||
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)){
<throw Exception>
}else{
echo $array[$index];
};
2: function f($p){ // without converting!!!
if(is_nan($p)){
<throw Exception>
}else{
return (int)$p; // actually a correct conversion to Integer would have to be checked
because the parser does not check this here.
};
}
----
Server IP: 45.112.84.4
Probable Submitter: 103.51.167.36 (proxied: 185.9.19.119)
----
Manual Page -- http://php.net/manual/en/math.constants.php
Edit -- https://master.php.net/note/edit/124871
Del: integrated -- https://master.php.net/note/delete/124871/integrated
Del: useless -- https://master.php.net/note/delete/124871/useless
Del: bad code -- https://master.php.net/note/delete/124871/bad+code
Del: spam -- https://master.php.net/note/delete/124871/spam
Del: non-english -- https://master.php.net/note/delete/124871/non-english
Del: in docs -- https://master.php.net/note/delete/124871/in+docs
Del: other reasons-- https://master.php.net/note/delete/124871
Reject -- https://master.php.net/note/reject/124871
Search -- https://master.php.net/manage/user-notes.php