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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 12 05:01:33 2025 UTC