修复PHP空函数 [英] Fixing the PHP empty function

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

问题描述

在使用 empty()函数时,PHP习惯评估(int)0和(string)0为空。如果您希望数值或字符串值为0,这可能会产生意想不到的结果。我怎样才能修复它只返回true为空对象,数组,字符串等?

PHP has the habit of evaluating (int)0 and (string)"0" as empty when using the empty() function. This can have unintended results if you expect numerical or string values of 0. How can I "fix" it to only return true to empty objects, arrays, strings, etc?

推荐答案

这并不适合我。

if (empty($variable) && '0' != $variable) {
  // Do something
}

我使用了:

if (empty($variable) && strlen($variable) == 0) {
  // Do something
}

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

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