PHP 认为 null 等于零

发布于 2024-08-12 07:20:26 字数 261 浏览 3 评论 0原文

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

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

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

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

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

But is there other elegant way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

煞人兵器 2024-08-19 07:20:26
$myvariable === 0

详细了解比较运算符

$myvariable === 0

read more about comparison operators.

空袭的梦i 2024-08-19 07:20:26

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

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

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

此页面告诉我们,如果要比较两个变量,那么变量将按如下方式转换(在第一个匹配处退出表)

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

因此您正在比较 null 与数字。因此,null 和数字都会转换为布尔值。 此页面告诉我们,在这样的转换中 null0 被视为 FALSE

现在,您的表达式为 false==false,这当然是 true。

但不是你想要的。

此页面提供了 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.

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

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

希望这有帮助。

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

Below, I will explain why what you are doing isn't working and how to fix it.

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

This page tells 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 page tells us that in such a conversion both null and 0 are considered FALSE.

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

But not what you want.

This page provides a list of PHP's comparison operators.

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.

Hope this helps.

作死小能手 2024-08-19 07:20:26

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

  • 如果变量是数字或数字字符串,则

    is_int($var)。要识别零,使用 is_numeric($var) 也是解决方案或使用 $var === 0

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

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

微暖i 2024-08-19 07:20:26

尝试 ($myvariable === 0) ,它不会执行类型强制。

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

捎一片雪花 2024-08-19 07:20:26

使用 php 函数 is_null( ) 函数以及 <代码>=== 运算符。 !== 也可以按照您期望的方式工作。

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

暮年 2024-08-19 07:20:26

第二种解决方案也行不通。 === 运算符是您问题的解决方案。

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

探春 2024-08-19 07:20:26

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

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

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

($myvariable === 0 || $myvariable === '0')
从﹋此江山别 2024-08-19 07:20:26

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

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

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.

    if (gettype($company_id) === 'NULL') {
        $company = Company::where('id', Auth::user()->company_id)->first();
    } else {
        $company = Company::where('id', $company_id)->first();
    }
戏舞 2024-08-19 07:20:26
$myvariable===0


$a === $b 

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

$myvariable===0


$a === $b 

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

软糯酥胸 2024-08-19 07:20:26

有一个 is_null 函数,但是这个只会替换您的 $myvariable!=null

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

老子叫无熙 2024-08-19 07:20:26

对于我的情况,我找到了这个解决方案,它对我有用:

if ($myvariable === NULL) {
    codehere...
}

For my case i found this soulution and it works for me :

if ($myvariable === NULL) {
    codehere...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文