在 php 中,0 是否被视为空? [英] In php, is 0 treated as empty?

查看:40
本文介绍了在 php 中,0 是否被视为空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码会解释更多:

$var = 0;

if (!empty($var)){
echo "Its not empty";
} else {
echo "Its empty";
}

结果返回Its empty".我认为 empty() 会检查我是否已经设置了变量并且里面有值.为什么它返回它是空的"??

The result returns "Its empty". I thought empty() will check if I already set the variable and have value inside. Why it returns "Its empty"??

推荐答案

http://php.net/empty

以下内容被认为是空的:

The following things are considered to be empty:

  • ""(空字符串)
  • 0(0 作为整数)
  • 0.0(0 作为浮点数)
  • "0" (0 作为字符串)
  • 错误
  • array()(空数组)
  • var $var;(声明了一个变量,但在类中没有值)

请注意,这与强制转换为布尔值 false 的列表完全相同.empty 就是 !isset($var) ||!$var.尝试使用 isset.

Note that this is exactly the same list as for a coercion to Boolean false. empty is simply !isset($var) || !$var. Try isset instead.

这篇关于在 php 中,0 是否被视为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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