逻辑运算符php true或false [英] Logical operators php true or false
问题描述
作为php neewbie,我尝试阅读很多其他人的代码以进行学习. 今天我遇到了这样的一行:
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 )
我想知道两者之间有什么区别
!==false
和==true
如果有人可以向我解释这一点,将不胜感激.
..如果没有真正的区别-使用引号中的一个而不是另一个使用引号的原因是什么?
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 ??
推荐答案
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
以上条件仅匹配值而不是数据类型,因此if
评估为TRUE
Above condition just match the values not the data type hence if
evaluate to TRUE
if (8 === '8') // returns false
并且此值同时检查值和值的数据类型,因此此if
评估为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.
就您而言,
stripos
返回子字符串在字符串中的位置,如果找不到字符串,则返回FALSE
.
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 )
上面的代码检查字符串内的子字符串,并仅在找到子字符串时对TRUE
求值.
如果将其更改为
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 )
,并且在0
位置找到子字符串时,即使主字符串中存在子字符串,if
也会评估为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 )
,由于0
被认为是FALSE
所以您必须在这里使用!==
So you have to use there !==
if ( 0 !== false )
这将比较两个值的值和数据类型
值0
是整数类型,而false
是boolean
类型,因此此处的数据类型不匹配,条件将为TRUE
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手册页指出了这些比较运算符,您应该检查一次.
PHP manual page states these comparison operator you should check this once.
这篇关于逻辑运算符php true或false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!