简体   繁体   中英

Strange usage of in_array

I stumbled upon this code:

in_array(($_GET['some_value']??-1),[])

and I'm having some trouble understanding it. My questions are as follows:

  1. What does the ?? operator mean in this context? My experience tells me it's similar to null coalescing but I'm not sure.
  2. What does in_array do if haystack is an empty array? Again, seems like it would always return FALSE but I'm new to PHP so I'd like confirmation on this.

That expression can be replaced with

false

calling in_array with an empty array as the second argument will always return false , so it doesn't matter whether $_GET['some_value'] exists, what it is if it exists, or whether or not it ends up getting replaced with negative one by the null coalescing operator.

You can't find anything in an empty array. It's probably either a mistake or an attempt at obfuscation.

The double question mark IS in fact the null coalesce operator, new in PHP 7: http://php.net/manual/de/migration70.new-features.php

in_array() will return false if haystack is an empty array, in fact it will only return TRUE if the needle is found in haystack. Read the documentation here:

http://php.net/manual/de/function.in-array.php

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