Checking for empty arrays: count vs empty

前端 未结 12 749
隐瞒了意图╮
隐瞒了意图╮ 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
    }
    

提交回复
热议问题