简体   繁体   English

in_array的奇怪用法

[英]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? 如果haystack是一个空数组, in_array做什么? Again, seems like it would always return FALSE but I'm new to PHP so I'd like confirmation on this. 同样,似乎它总是返回FALSE但是我是PHP新手,所以我想对此进行确认。

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. 以空数组作为第二个参数调用in_array始终将返回false ,因此$_GET['some_value']存在,它是否存在,是否最终被负数替换都$_GET['some_value']由空合并运算符。

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 实际上,双重问号是null合并运算符,这是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. 如果haystack是一个空数组,则in_array()将返回false,实际上,只有在haystack中找到该指针时,它才会返回TRUE。 Read the documentation here: 在此处阅读文档:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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