2

When I want to check if something is in the array and get the key back, I use the array_search() function.

Why is it when I compare the function to be exactly equal to true (=== true) it returns false, and when I compare it to not be exactly equal to false (!== false) it returns true?

<?php
    if(array_search($value, $array) === true)
    {
        // Fails
    }

    if(array_search($value, $array) !== false)
    {
        // Succeeds
    }
?>

Thanks in advance.

0

4 Answers 4

10

array_search returns you needle when a match is found. it returns false only when match is not found. This is why only the opposite works in your case.

Returns the key for needle if it is found in the array, FALSE otherwise.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahh, it makes sense now. Thanks Hasan.
4

Late to the party, but wanted to add some context / examples:

array_search will return the key (if the value is found) - which could be 0 - and will return FALSE if the value is not found. It never returns TRUE.

This code may sum it up better:

// test array...
$array = [
    0   => 'First Item',
    1   => 'Second Item',
    'x' => 'Associative Item'
];

// example results:
$key = array_search( 'First Item', $array );       // returns 0
$key = array_search( 'Second Item', $array );      // returns 1
$key = array_search( 'Associative Item', $array ); // returns 'x'
$key = array_search( 'Third Item', $array );       // returns FALSE

Since 0 is a falsey value, you wouldn't want to do something like if ( ! array_search(...) ) {... because it would fail on 0 index items.

Therefore, the way to use it is something like:

$key = array_search( 'Third Item', $array ); // returns FALSE
if ( FALSE !== $key ) {
    // item was found, key is in $index, do something here...
}

It's worth mentioning this is also true of functions like strpos and stripos, so it's a good pattern to get in the habit of following.

Comments

0

It will fail because if the call is succesfull it returns the key no true.

false is returned if it isnt found so === false is ok

from the manual:

Returns the key for needle if it is found in the array, FALSE otherwise.

Comments

0

array_search() does not return true.

If will only return false, if it can't find anything, otherwise it will return the key of the matched element.

According to the manual

array_search — Searches the array for a given value and returns the corresponding key if successful ....

Returns the key for needle if it is found in the array, FALSE otherwise.

4 Comments

As per @Hasan Khan, it does return FALSE
@allen213, It was not incorrect, you would understand better if I had written boolean
originally you wrote array_search does not return true of false
@allen213, I didn't disagree to that did I? I should have written array_search() does not return boolean. But, no body bothered to read the line after. Ha ha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.