How to determine if an array has any elements or not?

后端 未结 8 2014
青春惊慌失措
青春惊慌失措 2020-12-06 09:31

How do I find if an array has one or more elements?

I need to execute a block of code where the size of the array is greater than zero.

if ($result &         


        
8条回答
  •  一生所求
    2020-12-06 10:08

    If you want to only check if the array is not empty, you should use empty() - it is much faster than count(), and it is also more readable:

    if (!empty($result)) {
        // ...
    } else {
        // ...
    }
    

提交回复
热议问题