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

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

问题描述

代码将解释更多:

  $ var = 0; 

if(!empty($ var)){
echo它不是空的;
} else {
echo它的空;
}

结果返回它的空白。我以为empty()会检查我是否已经设置了变量并在里面有值。为什么它会返回它的空?

解决方案

http://php.net/empty


以下事项被认为是空的:


  • (空字符串)
  • 0(0为整数)

  • 0(0作为字符串)
  • NULL

  • FALSE

  • array()(一个空数组)

  • var $ var; (声明的变量,但在类中没有值)


请注意,这正是与强制转换为布尔值 false 相同的列表。 仅仅是!isset($ var)|| !是$ var 。改用 isset

Code will explain more:

$var = 0;

if (!empty($var)){
echo "Its not empty";
} else {
echo "Its 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:

  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)

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天全站免登陆