简体   繁体   中英

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

It is correct behavior.

There is no string /wp-content/uploads/2020/08/SJ-R42027-CZ-SG-1-scaled-1.jpg in that array.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM