skip to Main Content

I use PHP’s in_array() function.

The following PHP code example:

<?php

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

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

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.

2

Answers


  1. 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

    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.

    Note:

    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.

    Login or Signup to reply.
  2. 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 = ['apple', 'pear', 'banana', 'raspberry'];
    

    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('raspberry', $basket)) {
        $basket[] = 'raspberry';    
    }
    

    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('raspberry', $basket, true)) {
        $basket[] = 'raspberry';    
    }
    
    if (!in_array('raspberry', $basket, false)) {
        $basket[] = 'raspberry';    
    }
    

    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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search