php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #70798 array_search - positive match on $haystack[0] evalutes to boolean FALSE
Submitted: 2015-10-27 01:03 UTC Modified: 2015-10-27 02:44 UTC
From: andreas dot baumgaertner at tisco dot at Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 5.6.14 OS: Windows Server 2012 R2
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: andreas dot baumgaertner at tisco dot at
New email:
PHP Version: OS:

 

 [2015-10-27 01:03 UTC] andreas dot baumgaertner at tisco dot at
Description:
------------
array_search - positive match on $haystack[0] evalutes to boolean FALSE
the same value at integer array index 1 matches perfectly and evaluates to 1

--

change sample script to 
  $haystack[1] = 'German';
  $haystack[2] = 'English';

array_search('german',$haystack_lower) returns 1 now ...


Test script:
---------------
<?php

  $haystack = array();
  $haystack[0] = 'German';
  $haystack[1] = 'English';
  
  $haystack_lower = array_map('strtolower', $haystack);
  print_r($haystack);
  print_r('<br>');
  print_r($haystack_lower);
  print_r('<br>');
  if (array_search('german',$haystack_lower) != FALSE) { 
    print_r('match');
  }
  
?>



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2015-10-27 01:24 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2015-10-27 01:24 UTC] requinix@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

http://php.net/manual/en/function.array-search.php
> Warning: This function may return Boolean FALSE, but may also return a non-
> Boolean value which evaluates to FALSE. Please read the section on Booleans for
> more information. Use the === operator for testing the return value of this
> function.
 [2015-10-27 01:49 UTC] andreas dot baumgaertner at tisco dot at
I've read the documentation array_search ...
Returns the key for needle if it is found in the array, FALSE otherwise.

The issue is that the function returns FALSE instead of the key, when the match is at integer index [0].
I can't make the test script any shorter to proove my point.

The test script does a simple case insensitve compare using array_map / strtolower and array_search ...

--
I've taken the time to create the bug - at least take the time to run the test script!
 [2015-10-27 02:44 UTC] requinix@php.net
No, the function returned 0.

https://3v4l.org/JMTsG
https://3v4l.org/ui6hS
Pay close attention to what happens on line 12.

For even more reference material,

http://php.net/manual/en/language.types.boolean.php
> When converting to boolean, the following values are considered FALSE: [...]
> * the integer 0 (zero)

http://php.net/manual/en/types.comparisons.php
> Expression: $x = 0;
> boolean: FALSE
 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Oct 06 19:00:01 2026 UTC