how to check if an array has value that === null without looping? - php

http://php.net/manual/en/function.in-array.php - has issues with that since it checks only by ==. Is there any clever oneliner here that can do it by === ?
That is to return false if an array is empty, that is has 0 elements, or when array doesn't contain any value that is exactly null. True if array has at least one === null element.

in_array(null, $haystack, true);
If you read the doc you referenced, you'll see that that function takes an optional third parameter:
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.
Here is the function signature, specifically as it appears in the doc:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
Searches haystack for needle using loose comparison unless strict is set.

The in_array function accepts a third param (strict) that will do === comparison
in_array(null, $array, true);

Related

Is this a bug in PHP arrays? [duplicate]

I have found some odd behaviour while I was using the PHP function in_array(). I have an array like this:
$arr = [TRUE, "some string", "something else"];
Now if I want to check if "test" is in the array it is clearly not, but in_array() still returns TRUE, why is that?
$result = in_array("test", $arr);
var_dump($result); //Output: bool(true)
The same thing happens when using array_search():
$result = array_search("test", $arr);
var_dump($result); //Output: int(0)
I thought maybe that the value TRUE in the array was automatically causing the function to return TRUE for every result without checking the rest of the array, but I couldn't find any documentation that would suggest that very odd functionality.
This behaviour of the function in_array() and array_search() is not a bug, but instead well documented behaviour.
Both functions have a 3rd optional parameter called $strict which by default is FALSE:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
Now what that means is that by default both functions use loosely(==) comparison to compare the values. So they only check if the values are the same after PHP type juggling and without checking the type. Because of that in your example TRUE == "any none emtpy string" evaluates to TRUE.
So by setting the 3rd parameter to TRUE while calling the function you say that PHP should use strict(===) comparison and it should check value AND type of the values while comparing.
See this as a reference: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
You are right, the boolean can indeed cause this. Set the strict flag in the in_array function, this way also the type of the element is checked (basically the same as using ===):
if (in_array("username", $results, true)) // do something
if (in_array("password", $results, true)) // do something
if (in_array("birthday", $results, true)) // do something

Why does php in_array return true for an unmatched string value? Is this a bug? [duplicate]

I have found some odd behaviour while I was using the PHP function in_array(). I have an array like this:
$arr = [TRUE, "some string", "something else"];
Now if I want to check if "test" is in the array it is clearly not, but in_array() still returns TRUE, why is that?
$result = in_array("test", $arr);
var_dump($result); //Output: bool(true)
The same thing happens when using array_search():
$result = array_search("test", $arr);
var_dump($result); //Output: int(0)
I thought maybe that the value TRUE in the array was automatically causing the function to return TRUE for every result without checking the rest of the array, but I couldn't find any documentation that would suggest that very odd functionality.
This behaviour of the function in_array() and array_search() is not a bug, but instead well documented behaviour.
Both functions have a 3rd optional parameter called $strict which by default is FALSE:
bool in_array ( mixed $needle , array $haystack [, bool $strict = FALSE ] )
mixed array_search ( mixed $needle , array $haystack [, bool $strict = false ] )
Now what that means is that by default both functions use loosely(==) comparison to compare the values. So they only check if the values are the same after PHP type juggling and without checking the type. Because of that in your example TRUE == "any none emtpy string" evaluates to TRUE.
So by setting the 3rd parameter to TRUE while calling the function you say that PHP should use strict(===) comparison and it should check value AND type of the values while comparing.
See this as a reference: How do the PHP equality (== double equals) and identity (=== triple equals) comparison operators differ?
You are right, the boolean can indeed cause this. Set the strict flag in the in_array function, this way also the type of the element is checked (basically the same as using ===):
if (in_array("username", $results, true)) // do something
if (in_array("password", $results, true)) // do something
if (in_array("birthday", $results, true)) // do something

why does in_array(0,*) always return 1

why would in_array(0, array("pop")) return 1?
see docs clearly 0 is not a value in the array and this should return 0 or FALSE.
in_array($needle, $haystack, $strict = FALSE) searches haystack for needle using loose comparison unless strict is set.
When you don't provide third "strict" argument the values will be compared according to loose comparsion rules in php.
Go to http://php.net/manual/en/types.comparisons.php. You'll see this loose comparison behavior documented.
0 == "pop" is a loose comparison returning TRUE
0 === "pop" is a strict comparison returning FALSE

Why in_array function return TRUE when we are looking for an empty string? [duplicate]

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

I'm getting the strange output from in_array function [duplicate]

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

Categories