Checking for empty arrays: count vs empty

前端 未结 12 516
隐瞒了意图╮
隐瞒了意图╮ 2020-12-07 12:59

This question on \'How to tell if a PHP array is empty\' had me thinking of this question

Is there a reason that count should be used instead of e

相关标签:
12条回答
  • 2020-12-07 13:31

    My personal preference is more for coding elegance (in relation to my specific use-case). I agree with Dan McG inasmuch that count() isn't responding with the correct datatype (in this case boolean) for the test in question forcing the developer to write more code to fill an 'if' statement.

    Whether this has any significant impact on performance is only debatable for extremely large arrays (which you probably won't have enough memory allocation for anyway in most setups).

    Particularly when it comes to PHP's $_POST array, it seems much more "logical" in my opinion to write/see:

    if ( !empty ( $_POST ) ) {
        // deal with postdata
    }
    
    0 讨论(0)
  • 2020-12-07 13:32

    Is there a reason that count should be used instead of empty when determining if an array is empty or not?

    There is, when you need to do something on non-empty array knowing it's size:

    if( 0 < ( $cnt = count($array) ) )
    {
     echo "Your array size is: $cnt";
    }
    else
     echo "Too bad, your array is empty :(";
    

    But I wouldn't recommend using count, unless you are 100% sure, that what you are counting is an array. Lately I have been debugging code, where on error function was returning FALSE instead of empty array, and what I discovered was:

    var_dump(count(FALSE));
    

    output:

    int 1
    

    So since then I am using empty or if(array() === $array) to be sure that I have array that is empty.

    0 讨论(0)
  • There is no strong reason to prefer count($myArray) == 0 over empty($myArray). They have identical semantics. Some might find one more readable than the other. One might perform marginally better than the other but it's not likely to be a significant factor in the vast majority of php applications. For all practical purposes, the choice is a matter of taste.

    0 讨论(0)
  • 2020-12-07 13:39

    I generally use empty. Im not sure why people would use count really - If the array is large then count takes longer/has more overhead. If you simply need to know whether or not the array is empty then use empty.

    0 讨论(0)
  • 2020-12-07 13:41

    Sometimes using empty is a must. For example this code:

    $myarray = array();
    
    echo "myarray:"; var_dump($myarray); echo "<br>";
    echo "case1 count: ".count($myarray)."<br>";
    echo "case1 empty: ".empty($myarray)."<br>";
    
    $glob = glob('sdfsdfdsf.txt');
    
    echo "glob:"; var_dump($glob); echo "<br>";
    echo "case2 count: ".count($glob)."<br>";
    echo "case2 empty: ".empty($glob);
    

    If you run this code like this: http://phpfiddle.org/main/code/g9x-uwi

    You get this output:

    myarray:array(0) { } 
    case1 count: 0
    case1 empty: 1
    
    glob:bool(false) 
    case2 count: 1
    case2 empty: 1
    

    So if you count the empty glob output you get wrong output. You should check for emptiness.

    From glob documentation:

    Returns an array containing the matched files/directories, an empty array if no file matched or FALSE on error.
    Note: On some systems it is impossible to distinguish between empty match and an error.

    Also check this question: Why count(false) return 1?

    0 讨论(0)
  • 2020-12-07 13:43

    count() seems to work better with array-like interfaces that implement ArrayAccess/Countable. empty() returns true for these kinds of objects even if they have no elements. Typically these classes will implement the Countable interface, so if the question is "Does this collection contain elements?" without making an assumption about the implementation, then count() is a better option.

    0 讨论(0)
提交回复
热议问题