I am not getting what is wrong with this code. It's returning "Found", which it should not.
$lead = "418176000000069007";
$diff = array("418176000000069003","418176000000057001");
if (in_array($lead,$diff))
echo "Found";
else
echo "Not found";
It's because of one defect in PHP.
418176000000069007
is modified to 2147483647 (integer limit of PHP). That is why you are gettingFound
.try
in_array($lead, $diff, true)
If the third parameter strict is set to TRUE then the
in_array()
function will also check the types of the needle in the haystack, and because the limit is beyond the maximum integer value.So if PHP encounters a number beyond the bounds of the integer type, it will be interpreted as a float instead. Also, an operation which results in a number beyond the bounds of the integer type will return a float instead. Check the PHP manuals.
It is because of the limitations of the number storage in
PHP
,this was a bug and is rectified and solved in newer versions ofPHP
.The values exceed
PHP_INT_MAX
.Try to
echo
/print_r
$lead
and$diff
without using the quotes. It will resultso in this case the
in_array
result is true!so use
strict
comparison inin_array()
by setting third argument inin_array()
astrue
Try this. It will work.
in_array
should be stricted.This problem is due to your numbers are exceeded from the defined integer limit
Note: the maximum value depends on the system. 32 bit systems have a maximum signed integer range of
-2147483648
to2147483647
. So for example on such a system,intval('1000000000000')
will return2147483647
. The maximum signed integer value for 64 bit systems is9223372036854775807
.Try using brackets and use strict mode:
The values exceed
PHP_INT_MAX
. Try doingif(in_array($lead,$diff,true))
instead.