php 逻辑运算符 true 或 false
作为一名 php 新手,我尝试阅读很多其他人的代码来学习。 今天我遇到这样的一句话:
if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )
我想知道两者之间有什么区别 !==false
和 ==true
如果有人可以向我解释这一点,我将不胜感激。 ..如果没有真正的区别 - 使用引用的一个而不是另一个的原因是什么?
As a php neewbie , I try to read a lot of other people´s code in order to learn.
Today I came across a line like this :
if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )
I was wondering what is the difference between!==false
and ==true
If someone can explain that to me, It would be greatly appreciated.
..and if there is no real difference - what would be the reasons to use the quoted one over the other ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
PHP 是一种松散类型语言。
==
匹配两个值,===
匹配值以及值的数据类型。上述条件仅匹配值而不是数据类型,因此
if
评估为TRUE
并且此条件检查值和值的数据类型,因此此
if
计算结果为FALSE
时,您需要检查值和数据类型,请使用
===
;当您只需要比较时,请使用==
值不是数据类型。在您的情况下,
stripos
返回字符串中子字符串的位置,如果找不到字符串,则返回FALSE
。上面的代码检查字符串内的子字符串,并且仅当找到子字符串时才求值为
TRUE
。如果将其更改为
,并且当在
0
位置找到子字符串时,即使子字符串位于主字符串中,if
也会计算为 FALSE。那么条件将变得像这样
,这将评估为
FALSE
因为0
被视为FALSE
所以你必须在那里使用
!==
这将比较两个值的值和数据类型
值
0
是整数类型,而false
是boolean
类型,因此此处的数据类型不匹配,条件将为TRUE
PHP 手册页指出了这些比较运算符,您应该检查一次。
PHP is a loosely typed language.
==
match the both values and===
match the values as well as the data type of values.Above condition just match the values not the data type hence
if
evaluate toTRUE
and this one check both value and data type of values hence this
if
evaluate toFALSE
you use
===
where you want to check the value and data type both and use==
when you need to compare only values not the data type.In your case,
The
stripos
returns the position of the sub string in the string, if string not found it returnsFALSE
.The code above check sub string inside string and get evaluate to
TRUE
only when the sub string found.If you change it to
and when the sub string found at the
0
position theif
evaluate to FALSE even when sub string is there in the main string.Then the condition will become like this
and this will evaluate to
FALSE
because the0
is considered asFALSE
So you have to use there
!==
This will compare the values and data type of both values
The value
0
is an integer type and thefalse
isboolean
type, hence the data type does not match here and condition will beTRUE
PHP manual page states these comparison operator you should check this once.
!==false
和==true
之间的区别是 PHP 中相同/不相同和相等/不相等比较之间的区别。请参阅 PHP 手册中的比较运算符,它们之间有什么区别相同和等于是。
The difference between
!==false
and==true
is the difference between an identical/not-identical and equal/non-equal comparison in PHP.Please see the Comparison Operators in the PHP Manual what the difference between Identical and Equal is.
!==
是一种比较,不仅比较两个变量的值,而且还比较类型。此处使用它是因为当未找到命中时,
stripos
可以返回false
,而当在第一个字符中找到命中时,也可以返回0
。细绳。==
无法区分这两种情况(它们都是“falsy”),因此在使用stripos
=== >。 手册中有一条警告:!==
is a comparison that doesn't only compare the value, but also the type of both variables.It is used here because
stripos
can returnfalse
when no hit was found, but also0
when a hit was found in the first character of the string.==
is unable to distinguish those two cases (they are both "falsy"), so you have to use===
when working withstripos
. There's a warning in the manual:注意:==true 和 ===true 是不同的。
我认为!==false和===true类似,所以只解释==true和===true。对于第一个 ==,它的值相等,因此 1 == true,0==false。对于 ===,它在 PHP 中是“相同的”,即值相等,类型也相等。
因此,如果结果位于第 0 个位置,则结果应该为 true;但是,如果使用 ==true,则不会像 0!= true 那样工作。
例如,
stripos('一只羊', 'a')
如果你使用 ==true ,结果就像一开始一样是错误的。
Note: ==true and ===true are different.
I think !==false is similar to ===true, so only explain ==true and ===true. For the first ==, it is equal in value, thus 1 == true, 0==false. for ===, it is "identical" in PHP, namely, equal in value, and also in type.
thus, if the result is at 0th position, result should be true; however, if use ==true, it will not work as 0!= true.
For example,
stripos('a sheep', 'a')
if you use ==true, the result is wrong as it is at first place.