Home > Backend Development > PHP Tutorial > 这是不是多余的代码,该如何处理

这是不是多余的代码,该如何处理

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-13 12:19:49
Original
828 people have browsed it

这是不是多余的代码
数组:$list
if($list && count($list) >0){
            echo '不为空';
}else{
            echo '为空';
}
代码:count($list) >0是不是多余的?
------解决思路----------------------
如果在$list 与 count($list) >0之间做选择的话确实多余了,重复判断的目的是为了防止多种情况的发生!
------解决思路----------------------
基本上是多余的。如果确定是数组,$list与count($list) > 0基本是等价的。

不过需要注意的是:
虽然count函数设计的时候是用于计算数组的元素的个数的,但实际上也可以传递字符串作为参数:
$str = "";//空字符串
var_dump( $str );//string(0) ""
var_dump(count($str));//int(1)
当然,这与本题无关。。。可以忽略

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template