在 php 中,0 被视为空吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2220519/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:35:34  来源:igfitidea点击:

In php, is 0 treated as empty?

phpfunction

提问by mysqllearner

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"??

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

回答by deceze

http://php.net/empty

http://php.net/空

The following things are considered to be empty:

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

以下内容被认为是空的:

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

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

请注意,这与强制转换为 Boolean 的列表完全相同falseempty简直就是!isset($var) || !$var。试试吧isset

回答by Dan Soap

I was wondering why nobody suggested the extremely handy Type comparison table. It answers every question about the common functions and compare operators.

我想知道为什么没有人建议使用非常方便的Type 比较表。它回答了有关通用函数和比较运算符的所有问题。

A snippet:

一个片段:

Expression      | empty($x)
----------------+--------
$x = "";        | true    
$x = null       | true    
var $x;         | true    
$x is undefined | true    
$x = array();   | true    
$x = false;     | true    
$x = true;      | false   
$x = 1;         | false   
$x = 42;        | false   
$x = 0;         | true    
$x = -1;        | false   
$x = "1";       | false   
$x = "0";       | true    
$x = "-1";      | false   
$x = "php";     | false   
$x = "true";    | false   
$x = "false";   | false   

Along other cheatsheets, I always keep a hardcopy of this table on my desk in case I'm not sure

与其他备忘单一起,我总是在我的桌子上保留一张这张桌子的硬拷贝,以防我不确定

回答by Khandad Niazi

In case of numeric values you should use is_numeric function:

如果是数值,您应该使用 is_numeric 函数:

$var = 0;

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

回答by Calin Rusu

Use strlen()instead. I ran onto the same issue using 1/0 as possible values for some variables. I am using if (strlen($_POST['myValue']) == 0)to test if there is a character or not in my variable.

使用strlen()来代替。我使用 1/0 作为某些变量的可能值遇到了同样的问题。我if (strlen($_POST['myValue']) == 0)用来测试我的变量中是否有字符。

回答by Oliver Williams

I was recently caught with my pants down on this one as well. The issue we often deal with is unset variables - say a form element that may or may not have been there, but for many elements, 0(or the string '0'which would come through the post more accurately, but still would be evaluated as "falsey") is a legitimate value say on a dropdown list.

我最近也被抓到了我的裤子。我们经常处理的问题是未设置的变量——比如说一个表单元素可能存在也可能不存在,但对于许多元素,0(或者'0'更准确地通过帖子出现的字符串,但仍然会被评估为“虚假” ) 是下拉列表中的合法值。

using empty()first and then strlen()is your best best if you need this as well, as:

如果您也需要它,请先使用empty()然后strlen()是最好的,例如:

if(!empty($var) && strlen($var)){
    echo '"$var" is present and has a non-falsey value!';
}

回答by Elzo Valugi

From a linguistic point of view emptyhas a meaning of without value. Like the others said you'll have to use isset()in order to check if a variable has been defined, which is what you do.

从语言学的角度看,空有没有价值的意思。就像其他人所说的那样,您必须使用isset()它来检查是否已定义变量,这就是您所做的。

回答by soulmerge

empty()returns true for everything that evaluates to FALSE, it is actually a 'not' (!) in disguise. I think you meant isset()

empty()对于计算为 的所有内容都返回 true FALSE,它实际上是!伪装的“非”( )。我想你的意思是isset()

回答by bhu1st

To accept 0 as a value in variable use isset

在变量使用中接受 0 作为值 isset

Check if variable is empty

检查变量是否为空

$var = 0;

if ($var == '') {
    echo "empty";
} else {
    echo "not empty"; 
}
//output is empty

Check if variable is set

检查变量是否设置

$var = 0;

if (isset($var)) {
    echo "not empty";
} else {    
    echo "empty";
}
//output is not empty

回答by Toan Nguyen Thai

It 's working for me!

它对我有用!

//
if(isset($_POST['post_var'])&&$_POST['post_var']!==''){
echo $_POST['post_var'];
}

//results:
1 if $_POST['post_var']='1'
0 if $_POST['post_var']='0'
skip if $_POST['post_var']=''

回答by Adam Kiss

From manual: Returns FALSE if var has a non-empty and non-zero value.

来自手册:如果 var 具有非空和非零值,则返回 FALSE。

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)
  • ""(空字符串)
  • 0(0 作为整数)
  • "0" (0 作为字符串) NULL
  • FALSE array()(空数组)var
  • $var; (声明的变量,但在类中没有值)

More: http://php.net/manual/en/function.empty.php

更多:http: //php.net/manual/en/function.empty.php