php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #73345 loose type comparison fault affecting in_array
Submitted: 2016-10-19 08:09 UTC Modified: 2016-10-19 08:21 UTC
From: matt at h-bs dot co Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 7.0.12 OS: any
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: matt at h-bs dot co
New email:
PHP Version: OS:

 

 [2016-10-19 08:09 UTC] matt at h-bs dot co
Description:
------------
although this is documented in the loose comparison section @ http://php.net/manual/en/types.comparisons.php, the failure of php to evaluate any string (loosely) against numeric 0 correctly is causing issues in 'in_array', and possibly some others(?).

I realise that the problem can be worked around with strict flags/syntax, but it seems odd that this bug can be allowed to survive?

Maybe its my understanding of the core thats flawed, and theres a perfectly good explanation as to why a string evaluates to true against numeric 0, but it feels properly broken to me.

many thanks.

Matt


Test script:
---------------
$array = array(
    'test',
    0
              );
              
var_dump(in_array('xxx', $array));

var_dump('test' == 0);

Expected result:
----------------
false

false

Actual result:
--------------
true

true


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2016-10-19 08:21 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: Math related +Package: Arrays related
 [2016-10-19 08:21 UTC] requinix@php.net
As you've pointed out, the fact that in_array does loose comparisons by default is documented. And as you've pointed out, if you want a strict comparison then all you have to do is use that third parameter.

If you don't understand the behavior of loose comparisons, there's documentation for that.
http://php.net/manual/en/types.comparisons.php#types.comparisions-loose (which you've already seen)
http://php.net/manual/en/language.types.type-juggling.php
http://php.net/manual/en/language.types.string.php#language.types.string.conversion
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 14:01:30 2024 UTC