[英]Using PHP identical comparision operators with primitive types does make any sense?
Can't get the point of ===
and !==
with primitive types: 使用原始类型无法获得
===
和!==
:
$a === $b TRUE if $a is equal to $b, and they are of the same type.
$a !== $b TRUE if $a is not equal to $b, or they are not of the same type.
The assuming that $request->getMethod()
returns GET
or POST
(as string
) and that $form->isValid()
returns a boolean true
or false
, the following code: 假设
$request->getMethod()
返回GET
或POST
(作为string
),而$form->isValid()
返回布尔值true
或false
,则以下代码:
if('POST' === $request->getMethod() || (false === $form->isValid())) :
endif;
Does make any sense in respect of this shorter one: 关于这个较短的内容,是否有意义:
if('POST' == $request->getMethod() || !$form->isValid()) :
endif;
They are sometimes necessary. 它们有时是必需的。 For example when using
strpos
to check if a string is contained in another string you have to distinguish 0
from false
. 例如,当使用
strpos
检查一个字符串是否包含在另一个字符串中时,必须将0
与false
区分开。
wrong: 错误:
if(strpos($haystack,$needle))...
right: 对:
if(strpos($haystack,$needle) !== false)...
You have truty and falsy values in PHP. 您在PHP中具有真实和虚假的值。 For instance,
0
, ''
, array()
are falsy values. 例如,
0
, ''
, array()
是伪造的值。 If you use ==
it will match those values with the truty/falsy values: 如果使用
==
它将使这些值与truty / falsy值匹配:
var_dump(true == 'hello'); // true because a not empty string is a truty value
var_dump(false == 0); // true
===
will match not only the value but the type also: ===
将不仅匹配值,而且还匹配类型:
var_dump(true === 'hello'); // false, true is a boolean and 'hello' a string
var_dump(false === 0); // false, false is a boolean and 0 is a string
This will become a problem when a function can return 0
or false
, strpos
for example. 这会成为一个问题,当一个函数可以返回
0
或false
, strpos
例如。
There are also other factors with the ==
. ==
还有其他因素。 It will type cast values to a int
if you compare 2 different types: 如果您比较两种不同的类型,它将强制类型转换为
int
:
var_dump("123abc" == 123); // true, because '123abc' becomes `123`
This will be problematic if you compare a password: http://phpsadness.com/sad/47 如果您比较密码,这将是有问题的: http : //phpsadness.com/sad/47
==
will sometimes have odd behavior when comparing different types. 比较不同类型时,
==
有时会出现奇怪的行为。 Eg 'POST'
would be considered equal to 0
. 例如,
'POST'
将被视为等于0
。 That's why many people usually use ===
, it avoids type-juggling problems. 这就是为什么许多人通常使用
===
,它避免了类型变乱的问题。
In your case it shouldn't make a difference though. 在您的情况下,它应该不会有所作为。
although it may not be needed, 尽管可能不需要
(false===$form->isValid())
and 和
!$form->isValid()
are not the same as the first is checking to see if the value of $form->isValid()
is false, while the second is checking if $form->isValid()
is a falsey value, so for example if $form->isValid()
returns null
then the first statement will not evaluate to true while the second one will evluate to true. 与第一个检查
$form->isValid()
是否为false不同,第二个检查$form->isValid()
的值是否为falsey,例如$form->isValid()
返回null
则第一条语句的计算结果不为true,而第二条语句的计算结果为true。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.