rsk82
rsk82

Reputation: 29387

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

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.

Upvotes: 14

Views: 19963

Answers (2)

Rafael
Rafael

Reputation: 18522

The in_array function accepts a third param (strict) that will do === comparison

in_array(null, $array, true);

Upvotes: 8

user456814
user456814

Reputation:

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.

Upvotes: 39

Related Questions