根据php文档,以下表达式在调用时返回true empty($var)
我已经找到了如何通过使用,empty($var) && $var != 0但为什么PHP开发人员这样做"解决"问题?我认为这很荒谬,但是你有这个代码:
if (empty($_POST["X"])) {
doSomething();
}
Run Code Online (Sandbox Code Playgroud)
我认为"0"不是空的,空的是什么时候什么都没有!也许最好使用它
if (isset($x) && x != "") {//for strings
doSomething();
}
Run Code Online (Sandbox Code Playgroud) array_filter用php函数调用时你应该这样做
function myFunc($e) {
return something($e);
}
array_filter($myArray,"myFunc");
Run Code Online (Sandbox Code Playgroud)
但是我如何传递一个类方法呢?(例如,静态或非静态)
class A {
public function foo() {
//code
$a = array_filter($array,"self::myFilter");
//or if myFilter is an instance method
$a = array_filter($array,"this->myFilter");
}
public (static)? function myFilter($e) {return something($e);}
}
Run Code Online (Sandbox Code Playgroud)
我需要因为我将在类中的其他地方重用我的过滤器函数,我尝试在静态变量中使用匿名函数,但我得到错误