php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #49057 The in_array Function
Submitted: 2009-07-25 16:18 UTC Modified: 2009-07-25 17:58 UTC
From: admin at djokodonev dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: 5.2.10 OS: Windows XP
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: admin at djokodonev dot com
New email:
PHP Version: OS:

 

 [2009-07-25 16:18 UTC] admin at djokodonev dot com
Description:
------------
/*
This function should be changed...

 When i check if a value is in array I would expect it to behave like the equal $var1 == $var2, or $var1 === $var2 to check the types if they are equal in the strict mode.. 

The in_array function should be changed if you ask me... 
*/

$status = "on";

function is_valid(&$status) {
   $valids = array(0, 1);

   if (in_array($status, $valids)) {
       return true;
   }
   else if ( $status == "on") {
       $status = 1;
       return true;
   }
   return false;
}

var_dump(is_valid($status));// => true;
var_dump($status); // => it is a striung "on"; Meaning it was validated from the first part of the function where somehow it is found in the array with valid stings(int) ... 

/*

this returns true...

I guess i didn't expect it.. Maybe this is the way it should be?

When you use strings in the array ( => $valids = array("0", "1"); it returns the expected results. 

One of the notes on your page: 
Note: If needle  is a string, the comparison is done in a case-sensitive manner. I don't understand how this can be used for any purpose but to foll developers in getting wrong results from in_array check.. 


Regards

*/

Reproduce code:
---------------
$status = "on";

function is_valid(&$status) {
   $valids = array(0, 1);

   if (in_array($status, $valids)) {
       return true;
   }
   else if ( $status == "on") {
       $status = 1;
       return true;
   }
   return false;
}

var_dump(is_valid($status));// => true;
var_dump($status); // => it is a striung "on"; Meaning it was validated from the first part of the function where somehow it is found in the array with valid stings(int) ... 


/*

this returns true...

I guess i didn't expect it.. Maybe this is the way it should be?

When you use strings in the array ( => $valids = array("0", "1"); it returns the expected results. 

Just thought to report it. 

Regards

*/


Expected result:
----------------
The expected result is to get the string validated by the second part... and get it converted to 1 based to the reference call.. 


I read the documentation... 

And i see nowhere that if you use 0 or one in the array it returns true...

This is illogical.. 

And very very confusing... and reasonless.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-25 16:46 UTC] jani@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.


 [2009-07-25 17:58 UTC] rasmus@php.net
That's what the strict mode argument is for.  Without it you have == comparisons and with it you have strict === checks.  And since you are checking a string against an array of integers the == check casts your string to an integer.  'on'==0 which is in the array.  Change your code to use if(in_array($status, $valids, true)) and you will get your desired result.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Tue Jul 08 17:01:35 2025 UTC