php 运算符 == (某种相等)和整数 0

发布于 2024-12-09 06:20:26 字数 809 浏览 0 评论 0原文

php,我最亲爱的老朋友。

好的,所以我可以理解为什么字符串“0”是一个假值。这是公平的,因为 '0' 与松散类型语言中的 0 相同,而 0false 采用松散类型语言。所以这给出了 false == 0 == '0'

好吧好吧……但是!!这是怎么回事?

<?php
    print "number of surprised persons: " . ('false' == 0);

输出是......

number of surprised persons: 1

这怎么合理?我是唯一一个对此感到惊讶的人吗?我没看到什么?

进一步的测试证明整数 0 是相等的(通过运算符 ==),

0        <-- integer
false    <-- boolean
null     <-- just.. you know, null
'0'      <-- string
''       <-- string
'false'  <-- string
'true'   <-- string
'null'   <-- string

自然地,我主要使用运算符 === 来比较东西。既然我知道了这一点,我当然必须调整我的编程,这是毫无疑问的。但仍然!有人可以解释一下pl0x吗?

php, my dearest old frienemy.

ok, so i can come to terms with why the string '0' would be a falsie value. that's only fair seeing as how '0' is the same as 0 in a loosely typed language, and 0 is false in a loosely typed language. so this gives that false == 0 == '0'.

fine fine... BUT!! what is this all about?

<?php
    print "number of surprised persons: " . ('false' == 0);

the output is....

number of surprised persons: 1

how is this reasonable? am i the only one who's surprised by this? what am i failing to see?

further testing has proven that the integer 0 is equal (by operator ==) to

0        <-- integer
false    <-- boolean
null     <-- just.. you know, null
'0'      <-- string
''       <-- string
'false'  <-- string
'true'   <-- string
'null'   <-- string

naturally, i mostly use operator === to compare stuff. and now that i know about this, i'll have to adjust my programming of course, no question about that. but still! can someone shed some light pl0x?

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

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

发布评论

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

评论(3

败给现实 2024-12-16 06:20:26

这是因为,当您将字符串与整数进行比较时,它们不会同时转换为字符串或布尔值 - 它们会转换为整数。对于 PHP,当你仔细想想,这一点也不奇怪(相对而言,我想)。

'true' == 0
// is the same as
(int)'true' == 0
// is the same as
0 == 0
// is the same as
true

对于任何非数字字符串以及字符串 "0" 都是如此。之所以打印出1,是因为true的字符串版本是1(而false的字符串版本是空字符串)。

It's because, when you compare a string to an integer, they don't both get converted to strings, or to booleans - they get converted to integers. For PHP, when you think about it, this isn't strange at all (comparatively, I suppose).

'true' == 0
// is the same as
(int)'true' == 0
// is the same as
0 == 0
// is the same as
true

And this is true for any non-numeric string as well as the string "0". The reason 1 is printed out is because the string version of true is 1 (and the string version of false is an empty string).

祁梦 2024-12-16 06:20:26

就您关心的输出而言:

('false' == 0) = boolean TRUE = string "1"

echo 正在触发字符串上下文。

但从你下面的评论中我刚刚看到,你想了解更多有关比较的信息。看看你做了什么:

Example     Name     Result
$a == $b    Equal    TRUE if $a is equal to $b after type juggling.

你正在对数字与字符串进行非严格比较:

如果将数字与字符串进行比较,或者比较涉及数字字符串,则每个字符串都会转换为数字,并以数字方式进行比较。 (REF)

注意:数字,而不是接受答案中写入的整数:

<?php
print "number of surprised persons: " . ('false' == 0.3 - 0.2 - 0.1);

玩得开心。

As far as you're concerned about the output:

('false' == 0) = boolean TRUE = string "1".

echo is triggering string context.

But from your comment below I've just seen, that you'd like to learn more about the comparison. Take a look what you do:

Example     Name     Result
$a == $b    Equal    TRUE if $a is equal to $b after type juggling.

so you are doing a non-strict comparison of a number with a string:

If you compare a number with a string or the comparison involves numerical strings, then each string is converted to a number and the comparison performed numerically. (REF)

Note: Number, not integer as written in the accepted answer:

<?php
print "number of surprised persons: " . ('false' == 0.3 - 0.2 - 0.1);

Have fun.

心头的小情儿 2024-12-16 06:20:26

假 == 0 为真。 True 作为字符串为“1”,并且在插入值时正在进行隐式转换。

$a = true;
echo "$a"; #1

您可以在此处找到 PHP 真值表。我只是推荐 === 比较器,除非你有充分的理由使用 ==

http: //php.net/manual/en/types.comparisons.php

false == 0 is true. True as a string is '1' and you are doing an implicit conversion when you interpolate the value.

$a = true;
echo "$a"; #1

You can find a PHP truth table here. I would just recommend the === comparator unless you have a good reason to use ==

http://php.net/manual/en/types.comparisons.php

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