php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42903 in_array returns true in false case
Submitted: 2007-10-09 15:02 UTC Modified: 2007-10-09 16:33 UTC
From: tiramon at gmx dot net Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.2.4 OS: UNIX/WIN
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: tiramon at gmx dot net
New email:
PHP Version: OS:

 

 [2007-10-09 15:02 UTC] tiramon at gmx dot net
Description:
------------
if ( in_array( "1a", array(1) ) )
{
  echo "ok";
}

returns ok 

I know that it returns true if i compare "1" with 1 (string with int) and that there is a third parameter to avoid this.

Bot imho "1a" and 1 are completly different and should even without type compare return false

I tried this on a unix system with 5.2.0 and just to check if it was fixed in the current version with 5.2.4 at a win system

Reproduce code:
---------------
just execute 

if ( in_array( "1a", array(1) ) )
{
  echo "ok";
}

Expected result:
----------------
No Output

Actual result:
--------------
script echoed "ok"

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-09 16:33 UTC] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

Use the third parameter to enable strict typing when you compare strings and integers.

if ( in_array( "1a", array(1), true ) )
{
  echo "ok";
}

In this case "1a" is converted to an integer and the result will be "1"
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Jan 13 09:00:01 2026 UTC