Skip to content
Advertisement

Why does in_array return false for this search?

Why does below code return false? It ought to return true.

in_array(
    '/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg',
    [
        'https://example.com/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg'
    ],
    false
)

Online code editor

Advertisement

Answer

I think you have misunderstood the “strict” argument. Eg.

in_array('3', [1, 2, 3, 4], true); // ==> false because the string '3' !== 3

However if you don’t pass a third argument or pass it false:

in_array('3', [1, 2, 3, 4]);        // ==> true because '3' == 3
in_array('3', [1, 2, 3, 4], false); // ==> true because '3' == 3

However in your case the strings don’t match even with loose ==:

'/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg' == 
    'https://example.com/wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg'
// ==> false
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement