This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
Why does this return true.
$needle = TRUE;
$haystack = array('that', 'this');
print in_array($needle, $haystack); // 1
EDIT: I am aware that one can pass in_array() the strict parameter to check types. I want to know why specifically the behaviour I show is exhibited.
Any non-empty string in PHP is equal to TRUE when loose comparison is made (i.e. type is ignored). You may test this by doing:
var_dump('this' == TRUE);
var_dump('that' == TRUE);
DEMO
But the results are quite different when strict comparison is made (i.e. type is taken into consideration):
var_dump('this' === TRUE);
var_dump('that' === TRUE);
DEMO
In order to enforce strict comparison in the function in_array, you have to set the optional third parameter to TRUE:
$needle = TRUE;
$haystack = array('that', 'this');
var_dump(in_array($needle, $haystack, TRUE));
DEMO
Related
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 4 months ago.
I have an array of lower case hex strings. For some reason, in_array is finding matches where none exist:
$x = '000e286592';
$y = '0e06452425';
$testarray = [];
$testarray[] = $x;
if (in_array($y, $testarray)) {
echo "Array element ".$y." already exists in text array ";
}
print_r($testarray);exit();
The in_array comes back as true, as if '0e06452425' is already in the array. Which it is not. Why does this happen and how do I stop it from happening?
Odd. This has something to do with the way that PHP compares the values in "non-strict" mode; passing the third argument (strict) as true no longer results in a false positive:
if (in_array($y, $testarray, true)) {
echo "Array element ".$y." already exists in text array ";
}
I'm still trying to figure out why, technically speaking, this doesn't work without strict checking enabled.
You can pass a boolean as third parameter to in_array() to enable PHP strict comparison instead of the default loose comparison.
Try this code
$x = '000e286592';
$y = '0e06452425';
$testarray = [];
$testarray[] = $x;
if (in_array($y, $testarray, true)) {
echo "Array element ".$y." already exists in text array ";
}
print_r($testarray);exit();
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
The PHP function in_array(...) "checks if a value exists in an array".
But I'm observing a very strange behavior on handling strings (PHP v7.0.3). This code
$needle = 'a';
$haystacks = [['a'], ['b'], [123], [0]];
foreach ($haystacks as $haystack) {
$needleIsInHaystack = in_array($needle, $haystack);
var_dump($needleIsInHaystack);
}
generates following output:
bool(true)
bool(false)
bool(false)
bool(true) <- WHAT?
The function returns true for every string $needle, if the $haystack contains an element with the value 0!
Is it really by design? Or is it a bug and should be reported?
If you do not set the third parameter of in_array to true, comparison is done using type coercion.
If the third parameter strict is set to TRUE then the in_array() function will also check the types of the needle in the haystack.
Under loose comparison rules, effectively 'a' is equal to 0 since (int)'a' == 0.
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
Could someone explain to me why that is true?
in_array('', array(0,1,2));
Because, as said in the docs:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Searches haystack for needle using loose comparison unless strict is set.
... and '' == 0 is true in PHP. If you want to use strict comparison, just call in_array() with three params:
in_array('', array(0, 1, 2), true); // false
... so the types will be checked as well, and String '' won't have a chance to match against Numbers.
in_array by default performs loose comparison. Thus '' is equivalent to 0.
There is third argument (boolean) to in_array function which says if the matching is to be performed in STRICT way or not.
if you do in_array('', array(0,1,2), TRUE); then the result will be false.
Refer the documentation
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
in php the following code returns true
$array = array(
'isReady' => false,
'isPHP' => true,
'isStrange' => true
);
var_dump(in_array('sitepoint.com', $array));
result is a true WHY ?
Because of the 3rd parameter to in_array, $strict.
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
By default it's FALSE, which makes it use ==.
'sitepoint.com' == true
That's actually true (because of PHP's type juggling)!
You want to do:
in_array('sitepoint.com', $array, TRUE);
That will make it use ===.
'sitepoint.com' === true
That's not true.
in_array performs loose comparison (value checking only, not type & value). Since your array values are all booleans (true and false), the search string ("sitepoint.com") is being coerced to a boolean, effectively, your code translates to:
var_dump(in_array((bool)'sitepoint.com', array(true, false, true)));
Since a string, when cast to bool is true, in_array returns true.
To force type and value checking, pass true as third argument to in_array.
cf the docs
This question already has answers here:
PHP in_array() / array_search() odd behaviour
(2 answers)
Closed 6 years ago.
$arrValue = array('first', 'second');
$ret = in_array(0, $arrValue);
var_dump($ret);
var_dump($arrValue);
Above example gives following result:
bool(true)
array(2) {
[0]=> string(5) "first"
[1]=> string(6) "second"
}
Why in_array() matches needle 0 to any given haystack?
That's because the function uses a non-strict comparison. The string in the array is compared to integer 0. Some typecasting is happening with data loss, and both are regarded the same:
var_dump(0 == 'first'); // bool(true)
So solve this, you can use the third parameter and set it to true to request strict comparison.
$ret = in_array(0, $arrValue, true);
Keep in mind, through, that strict is really strict. In a strict comparison, 0 is not equal to "0".
Docs: http://nl3.php.net/in_array
Basically here 0 treats as false, so the search will occur like the function in_array search false between your array value. Make it(0) string to get different output.
As php a support strict or non-strict comparison so you need to pass a third value true it tell the to be strict because by default it is non-strict.