Can I ommit the strict parameter in PHP in_array() when all objects are of the same type?

huangapple go评论77阅读模式
英文:

Can I ommit the strict parameter in PHP in_array() when all objects are of the same type?

问题

我使用 PHP 的 in_array() 函数。

以下是 PHP 代码示例:

<?php

$basket = [ 'apple', 'pear', 'banana' ];

if (!in_array('raspberry', $basket)) {
    $basket[] = 'raspberry';    
}

var_dump($basket);

你建议是否指定 strict = true 参数?

我认为这并不必要,因为可以确保数组中只包含字符串(没有 null 或其他类型的内容)。
此外,该函数的第一个参数已经确保是一个字符串。
因为我不确定,想知道是否仅在数组中包含不同类型的对象和参数可能是不同类型时才指定 strict = true 是正确的做法。

期待听到社区的更多意见。

英文:

I use PHP's in_array() function.

The following PHP code example:

&lt;?php

$basket = [ &#39;apple&#39;, &#39;pear&#39;, &#39;banana&#39; ];

if (!in_array(&#39;raspberry&#39;, $basket)) {
    $basket[] = &#39;raspberry&#39;;    
}

var_dump($basket);

Do you suggest to specify the strict = true parameter?

I think it is not necessary because it is ensured that there are only strings in the array (no null, nor something of other type).
Also the 1st argument of the function is ensured to be a string.
Because I am unsure and would like to know if it is correct to specify strict = true only for cases when there objects in the array of various types and the argument might be of a different type.

Looking forward to hear more from the community here.

答案1

得分: 1

你在关于strict参数的逻辑方面大多是正确的。首先,我很少使用它,因为在我的实际代码库中很少涉及混合数组,当我不得不处理混合数组时,我更喜欢在进行比较之前将它们转换为特定类型。

根据php.netin_array函数的文档,这是strict参数的定义:

strict

如果将第三个参数strict设置为true,则in_array()函数还将检查在haystack中的needle的类型。

注意:

在PHP 8.0.0之前,在非严格模式下,字符串needle将与数组值0匹配,反之亦然。这可能导致不希望的结果。其他类型也存在类似的边缘情况。如果不能绝对确定涉及的值的类型,请始终使用strict标志以避免意外行为。

根据这个定义和你的用例,你不需要传递strict=true以使比较正确工作。

英文:

You are mostly correct in your logic about the strict parameters. For one, I rarely use it because I rarely deal with mixed arrays in my actual codebase and when I have to deal with mixed array, I prefer to convert them to a particular type before doing comparison.

As per the documentation of in_array on php.net, here is the definition of the strict parameter:

>strict
><br/>
If the third parameter strict is set to true then the in_array() function will also check the types of the needle in the haystack.
><br/>
>Note:
><br/>
>Prior to PHP 8.0.0, a string needle will match an array value of 0 in non-strict mode, and vice versa. That may lead to undesirable results. Similar edge cases exist for other types, as well. If not absolutely certain of the types of values involved, always use the strict flag to avoid unexpected behavior.

According to this definition and your use case, you do not need to pass strict=true for the comparison to work correctly.

答案2

得分: 1

请注意,以下是您要求的代码部分的翻译:

&gt; Do you suggest to specify the strict = true parameter? [of in_array()]

对于您在问题中提到的情况,不建议使用`in_array()`,因此也不建议使用`strict = true`参数(以及`if`等)。而是应该这样做:

~~~php
$basket = ['apple', 'pear', 'banana', 'raspberry'];
~~~

但是,在您的写作中,如果要严格说的话,也可以这样说:

&gt; 仅在数组中存在不同类型的对象且参数可能是不同类型时才指定strict = true是否正确?

是的,这是正确的,特别是当所有值都具有相同的类型,并且对于该类型,弱(`==`)比较和严格(`===`)比较之间没有区别时(提示:这可以根据类型而异,例如对象、数组,在您的示例中是字符串类型的情况下是正确的)。

~~~php
if (!in_array('raspberry', $basket)) {
    $basket[] = 'raspberry';    
}
~~~

因此,经验法则是,当您希望或意味着严格比较时,将其指定为`true`,以便代码更好地传达意图。对于`false`也是如此。

在您的示例中,您无需验证第一个参数的类型,以查明是否启用了严格比较,例如,如果您使用了$strict = true。只需查看它,因为它写在第三个参数中。

~~~php
if (!in_array('raspberry', $basket, true)) {
    $basket[] = 'raspberry';    
}
~~~

~~~php
if (!in_array('raspberry', $basket, false)) {
    $basket[] = 'raspberry';    
}
~~~

如果不指定它并在以后重新访问代码,将不再清楚原始作者的意图。因此,无论是true还是false,指定第三个参数都使代码更易于维护。

https://php.net/in_array
英文:

> Do you suggest to specify the strict = true parameter? [of in_array()]

For the case you outline in the question, no, but only because I would not even suggest to use in_array() there at all (and the if etc.), but just:

$basket = [&#39;apple&#39;, &#39;pear&#39;, &#39;banana&#39;, &#39;raspberry&#39;];

But not without confirming your writing with a "strictly speaking, yes":

> [Is it] correct to specify strict = true only for cases when there [are] objects in the array of various types and the argument might be of a different type [?]

Yes, this is correct, especially when all values have the same type and there is no difference between weak (==) and strict (===) comparison for that type (hint: this can differ per type, e.g. object, array and it is true in your example with the string type).

if (!in_array(&#39;raspberry&#39;, $basket)) {
    $basket[] = &#39;raspberry&#39;;    
}

Therefore, the rule of thumb is to specify it as true like always, when you want or mean strict comparison. This is so that your code communicates better. And vice-versa for false.

In your example, you won't need to verify the types of the first parameters to find out whether or not strict comparison is in effect, for example, if you would have used $strict = true. You just see it, because it is written in the third parameter.

if (!in_array(&#39;raspberry&#39;, $basket, true)) {
    $basket[] = &#39;raspberry&#39;;    
}
if (!in_array(&#39;raspberry&#39;, $basket, false)) {
    $basket[] = &#39;raspberry&#39;;    
}

When not specifying it and revisiting the code later, it is not clear any longer what was intended by the original author. So it makes the code easier to maintain when you specify the third parameter, regardless of true or false.

https://php.net/in_array

huangapple
  • 本文由 发表于 2023年7月13日 23:09:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76680933.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定