这应该做你想要的:
function notempty($var) {
return ($var==="0"||$var);
}
编辑:我想表只能在预览中工作,而不是在实际的答案提交。所以请参考PHP type comparison tables了解更多信息。
notempty("") : false
notempty(null) : false
notempty(undefined): false
notempty(array()) : false
notempty(false) : false
notempty(true) : true
notempty(1) : true
notempty(0) : false
notempty(-1) : true
notempty("1") : true
notempty("0") : true
notempty("php") : true
对于除“0”之外的所有值,notempty()与!empty()相同,为true返回true。
编辑:如果您使用error_reporting(E_ALL),则无法通过值将未定义的变量传递给自定义函数。正如墨西哥人所指出的那样,你应该始终使用E_ALL来遵守最佳做法。 This link(评论#11)他提供了讨论为什么你不应该使用任何形式的错误抑制性能和可维护性/调试原因。
有关如何将参数传递给自定义函数by reference,请参阅orlandu63的答案。