Bug #70798 [Nab]: array_search - positive match on $haystack[0] evalutes to boolean FALSE
| From: | requinix@php.net | Date: | Tue, 27 Oct 2015 02:44:11 +0000 |
| Subject: | Bug #70798 [Nab]: array_search - positive match on $haystack[0] evalutes to boolean FALSE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-196837@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=70798&edit=1
ID: 70798
Updated by: requinix@php.net
Reported by: andreas dot baumgaertner at tisco dot at
Summary: array_search - positive match on $haystack[0]
evalutes to boolean FALSE
Status: Not a bug
Type: Bug
Package: *General Issues
Operating System: Windows Server 2012 R2
PHP Version: 5.6.14
Block user comment: N
Private report: N
New Comment:
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
Previous Comments:
------------------------------------------------------------------------
[2015-10-27 01:49:00] 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 01:24:29] 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:03:52] 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');
}
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=70798&edit=1