Checking for empty arrays: count vs empty

前端 未结 12 522
隐瞒了意图╮
隐瞒了意图╮ 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:48

    Alternatively, you can cast the variable as a boolean (implicitly or explicitly):

    if( $value )
    {
      // array is not empty
    }
    
    if( (bool) $value )
    {
      // array is still not empty
    }
    

    This method does generate an E_NOTICE if the variable is not defined, similarly to count().

    For more information, see the PHP Manual page on type comparisons.

提交回复
热议问题