php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74133 PHP function in_array() return wrong.
Submitted: 2017-02-20 06:29 UTC Modified: 2017-02-20 09:57 UTC
From: long dot it dot stu at gmail dot com Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.6.30 OS: Centos
Private report: No CVE-ID: None
 [2017-02-20 06:29 UTC] long dot it dot stu at gmail dot com
Description:
------------
---
From manual page: http://www.php.net/security.globals
---
PHP function in_array() return False although it existing.


Test script:
---------------
<?php
$array_1 = range(1, 1.9, 0.1);
var_dump(in_array(1.7, $array_1));
array_2 = range(0, 0.9, 0.1);
var_dump(in_array(0.3, $array_2));
var_dump(in_array(0.6, $array_2));
var_dump(in_array(0.7, $array_2));

//=>it return false;


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-02-20 08:20 UTC] requinix@php.net
-Status: Open +Status: Feedback -Package: PHP options/info functions +Package: Arrays related
 [2017-02-20 08:20 UTC] requinix@php.net
None of those four values exist in either of those two arrays.
 [2017-02-20 09:09 UTC] long dot it dot stu at gmail dot com
with function range(1, 1.9, 0.1) it return result under:
Array
(
    [0] => 1
    [1] => 1.1
    [2] => 1.2
    [3] => 1.3
    [4] => 1.4
    [5] => 1.5
    [6] => 1.6
    [7] => 1.7
    [8] => 1.8
)
when i check in_array(1.7, range(1, 1.9, 0.1));
it return False;
But all item in array except 1.7 it return False.
in range(0, 0.9, 0.1) it resut too.
 [2017-02-20 09:57 UTC] requinix@php.net
-Status: Feedback +Status: Not a bug
 [2017-02-20 09:57 UTC] requinix@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.

Oh, sorry, I mistook how the range() worked.

https://3v4l.org/Nb2iK
The values in the arrays aren't what you think because floating-point numbers aren't always accurate. You could round each value in the array, or use count+array_filter instead, or do something else.
https://3v4l.org/JK901
https://3v4l.org/r8Pq8
https://3v4l.org/dZq7U
 [2017-02-20 11:33 UTC] long dot it dot stu at gmail dot com
Hi team!. it's clearly. thanks for your feedback!.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 17:01:30 2024 UTC