php 逻辑运算符 true 或 false

发布于 2024-12-25 10:24:28 字数 276 浏览 7 评论 0原文

作为一名 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 技术交流群。

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

发布评论

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

评论(4

執念 2025-01-01 10:24:28

PHP 是一种松散类型语言。 == 匹配两个值,=== 匹配值以及值的数据类型。

if (8 == '8') // returns true

上述条件仅匹配值而不是数据类型,因此 if 评估为 TRUE

if (8 === '8') // returns false

并且此条件检查值和值的数据类型,因此此 if计算结果为 FALSE

,您需要检查值和数据类型,请使用 ===;当您只需要比较时,请使用 ==值不是数据类型。

在您的情况下,

stripos 返回字符串中子字符串的位置,如果找不到字符串,则返回FALSE

if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )

上面的代码检查字符串内的子字符串,并且仅当找到子字符串时才求值为 TRUE
如果将其更改为

if ( stripos($post_to_check->post_content, '[' . $shortcode) != false )

,并且当在 0 位置找到子字符串时,即使子字符串位于主字符串中,if 也会计算为 FALSE。
那么条件将变得像这样

if ( 0 != false )

,这将评估为 FALSE 因为 0 被视为 FALSE

所以你必须在那里使用 !==

if ( 0 !== false )

这将比较两个值的值和数据类型
0 是整数类型,而 falseboolean 类型,因此此处的数据类型不匹配,条件将为 TRUE

PHP 手册页指出了这些比较运算符,您应该检查一次。

PHP is a loosely typed language. == match the both values and === match the values as well as the data type of values.

if (8 == '8') // returns true

Above condition just match the values not the data type hence if evaluate to TRUE

if (8 === '8') // returns false

and this one check both value and data type of values hence this if evaluate to FALSE

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 returns FALSE.

if ( stripos($post_to_check->post_content, '[' . $shortcode) !== false )

The code above check sub string inside string and get evaluate to TRUE only when the sub string found.
If you change it to

if ( stripos($post_to_check->post_content, '[' . $shortcode) != false )

and when the sub string found at the 0 position the if evaluate to FALSE even when sub string is there in the main string.
Then the condition will become like this

if ( 0 != false )

and this will evaluate to FALSE because the 0 is considered as FALSE

So you have to use there !==

if ( 0 !== false )

This will compare the values and data type of both values
The value 0 is an integer type and the false is boolean type, hence the data type does not match here and condition will be TRUE

PHP manual page states these comparison operator you should check this once.

柳若烟 2025-01-01 10:24:28

!==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.

浪荡不羁 2025-01-01 10:24:28

!== 是一种比较,不仅比较两个变量的,而且还比较类型

此处使用它是因为当未找到命中时,stripos 可以返回 false,而当在第一个字符中找到命中时,也可以返回 0。细绳。

== 无法区分这两种情况(它们都是“falsy”),因此在使用 stripos=== >。 手册中有一条警告:

此函数可能返回布尔值 FALSE,但也可能返回计算结果为 FALSE 的非布尔值,例如 0 或“”。请阅读有关布尔值的部分以获取更多信息。使用 === 运算符来测试该函数的返回值。

!== is a comparison that doesn't only compare the value, but also the type of both variables.

It is used here because stripos can return false when no hit was found, but also 0 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 with stripos. There's a warning in the manual:

This function may return Boolean FALSE, but may also return a non-Boolean value which evaluates to FALSE, such as 0 or "". Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

两个我 2025-01-01 10:24:28

注意:==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.

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