检查空数组:数与空 [英] Checking for empty arrays: count vs empty

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

问题描述

在<一个这个问题href=\"http://stackoverflow.com/questions/2216052/how-to-tell-if-a-php-array-is-empty/2216062#2216062\">How判断一个PHP数组是空'有我在想这个问题。

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 empty when determining if an array is empty or not?

我个人的想法是,如果2是等效于空数组的情况下,你应该使用,因为它给出了一个布尔回答一个问题布尔值。从上面链接的问题,似乎计数($ VAR)== 0 是常用的方法。对我来说,虽然在技术上是正确的,是没有意义的。例如。 问:是$ var,你是空的?答:7 。嗯...

My personal thought would be if the 2 are equivalent for the case of empty arrays you should use empty because it gives a boolean answer to a boolean question. From the question linked above, it seems that count($var) == 0 is the popular method. To me, while technically correct, makes no sense. E.g. Q: $var, are you empty? A: 7. Hmmm...

有一个原因,我应该使用计数== 0 来代替,或只是一个个人喜好啦?

Is there a reason I should use count == 0 instead or just a matter of personal taste?

由于在评论中指出别人的,现在删除的答案,计数将有大型阵列的性能影响,因为它会计算所有的元素,而,因为它知道这是不是空能够尽快停止。所以,如果他们让在这个情况下,同样的结果,但计数可能是低效的,为什么我们曾经用计数($ VAR)== 0

As pointed out by others in comments for a now deleted answer, count will have performance impacts for large arrays because it will have to count all elements, whereas empty can stop as soon as it knows it isn't empty. So, if they give the same results in this case, but count is potentially inefficient, why would we ever use count($var) == 0?

推荐答案

我一般使用。林不知道人们为什么会使用count真的 - 如果数组大再算上时间较长/有更多的开销。如果你只需要知道与否的数组为空,然后使用空的。

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.

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

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