PHP 认为 null 等于零

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1782759/
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 03:49:19  来源:igfitidea点击:

PHP considers null is equal to zero

phpnull

提问by Steven

In php, ($myvariable==0)When $myvariable is zero, the value of the expression is true; when $myvariable is null, the value of this expression is also true. How can I exclude the second case? I mean I want the expression to be true only when $myvariable is zero. Of course I can write

在php中,($myvariable==0)当$myvariable为零时,表达式的值为真;当 $myvariable 为 null 时,此表达式的值也为 true。如何排除第二种情况?我的意思是我希望表达式仅在 $myvariable 为零时才为真。我当然可以写

($myvariable!=null && $myvariable==0 )

but is there other elegant way to do this?

但是还有其他优雅的方式来做到这一点吗?

回答by SilentGhost

$myvariable === 0

read more about comparison operators.

阅读更多关于比较运算符的信息

回答by Richard

You hint at a deep question: when should an expression be true?

你暗示了一个深刻的问题:表达式什么时候应该是真的?

Below, I will explain whywhat you are doing isn't working and howto fix it.

下面,我将解释为什么你正在做的事情不起作用以及如何解决它。

In many languages null, 0, and the empty string ("") all evaluate to false, this can make ifstatements quite succinct and intuitive, but null, 0, and ""are also all of different types. How should they be compared?

在许多语言中null0和空字符串("")都计算为假,这可以使if语句比较简洁的,直观的,但是null0""都是不同的类型也。他们应该如何比较?

This pagetells us that if we have two variables being compared, then the variables are converted as follows (exiting the table at the first match)

这个页面告诉我们,如果我们有两个变量进行比较,那么变量转换如下(在第一次匹配时退出表)

Type of First  Type of Second   Then
null/string    string           Convert NULL to "", numerical/lexical comparison
bool/null      anything         Convert to bool, FALSE < TRUE

So you are comparing a null versus a number. Therefore, both the null and the number are converted to boolean. This pagetells us that in such a conversion both nulland 0are considered FALSE.

所以你正在比较一个空值和一个数字。因此,null 和数字都转换为布尔值。这个页面告诉我们,在这样的转换中,null0都被考虑FALSE

Your expression now reads, false==false, which, of course, is true.

您的表达式现在显示为,false==false,这当然是正确的。

But not what you want.

但不是你想要的。

This pageprovides a list of PHP's comparison operators.

此页面提供了 PHP 的比较运算符列表。

Example   Name                Result
$a ==  $b  Equal              TRUE if $a equals $b after type juggling.
$a === $b  Identical          TRUE if $a equals $b, AND they are of the same type.
$a !=  $b  Not equal          TRUE if $a not equals $b after type juggling.
$a <>  $b  Not equal          TRUE if $a not equals $b after type juggling.
$a !== $b  Not identical      TRUE if $a not equals $b, or they are not of the same type.
$a  <  $b  Less than          TRUE if $a is strictly less than $b.
$a  >  $b  Greater than       TRUE if $a is strictly greater than $b.
$a <=  $b  Less than/equal    TRUE if $a is less than or equal to $b.
$a >=  $b  Greater than/equal TRUE if $a is greater than or equal to $b.

The first comparator is the comparison you are using now. Note that it performs the conversions I mentioned earlier.

第一个比较器是您现在使用的比较。请注意,它执行我之前提到的转换。

Using the second comparator will fix your problem. Since a null and a number are not of the same type, the ===comparison will return false, rather than performing type conversion as the ==operator would.

使用第二个比较器将解决您的问题。由于 null 和数字的类型不同,因此===比较将返回 false,而不是像==运算符那样执行类型转换。

Hope this helps.

希望这可以帮助。

回答by Pao Im

To identify as null or zero by:

通过以下方式标识为空或零:

  • is_int($var)if a variable is a number or a numeric string. To identify Zero, use is_numeric($var)is also the solution or use $var === 0

  • is_null($var)if a variable is NULL

  • is_int($var)如果变量是数字或数字字符串。识别零,使用is_numeric($var)也是解决方案或使用$var === 0

  • is_null($var)如果变量为 NULL

回答by Skilldrick

Try ($myvariable === 0)which will not perform type coercion.

试试($myvariable === 0)哪个不会执行类型强制。

回答by Salman A

Use the php function is_null( )function along with the ===operator. !==also works the way you'd expect.

将 php 函数is_null( )函数与===运算符一起使用。!==也以您期望的方式工作。

回答by Thermech

If your zero could be a string, you should also considere checking the "zero string"

如果您的零可能是一个字符串,您还应该考虑检查“零字符串”

($myvariable === 0 || $myvariable === '0')

回答by soulmerge

The second solution wouldn't work either. The ===operatoris the solution to your problem.

第二种解决方案也行不通。该===运营商是解决你的问题。

回答by Abhishek Deshpande

I hade faced a similar issue in one of my projects, with a minor difference that I was also using the values ZERO as a valid value for my condition. here's how I solved it using simple logic to separate NULL from zero and other values.

我在我的一个项目中遇到了类似的问题,有一个细微的区别,即我还使用零值作为我的条件的有效值。这是我如何使用简单的逻辑将 NULL 与零和其他值分开来解决它。

    if (gettype($company_id) === 'NULL') {
        $company = Company::where('id', Auth::user()->company_id)->first();
    } else {
        $company = Company::where('id', $company_id)->first();
    }

回答by Haim Evgi

$myvariable===0


$a === $b 

Identical TRUE if $a is equal to $b, and they are of the same type

如果 $a 等于 $b,并且它们的类型相同,则为相同的 TRUE

回答by Soufiane Hassou

There's an is_nullfunction, but this will just replace your $myvariable!=null

有一个is_null功能,但这只会取代你的$myvariable!=null