检查PHP数组是否为空的最佳方法 [英] Best way to check if PHP Array is empty

查看:93
本文介绍了检查PHP数组是否为空的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这主要是肤浅的,但是检查数组是否为空的最佳方法(执行速度更快,而不是个人喜好或可读性)是什么:

This is mostly superficial but what's the best (faster performing, not personal preference or readability) way to check if an array is empty:

  1. count($arr) == 0
  2. empty($arr)
  3. $arr === array()
  4. 其他?
  1. count($arr) == 0
  2. empty($arr)
  3. $arr === array()
  4. Other?

我的猜测是

  1. 尽可能地迭代然后返回
  2. 在检查变量是否为数组后仅执行1
  3. 似乎它应该很慢,因为它必须构造一个新的对象进行比较

但这并不能说明它在此处执行的任何编译时优化.

But that doesn't account for any compile time optimizations that it performs here.

我不会在代码库中将所有实例更改为最快的方法,所以请不要提醒我这是微优化.这很简单.

I'm not about to go through my code base changes all instances to the fastest possible method so please don't remind me it's micro optimzation. This is simple curiosity.

推荐答案

    if(empty($arr))
    echo "Empty";
    else
    echo "Ok..!";

这是检查阵列是否为空的最快,安全的方法

This is the fastest and secure way to check an array empty or not

这篇关于检查PHP数组是否为空的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆