if(某事) vs if(某事===true)
我正在自学 cakephp(版本 1.26)。
我得到了一个简单的 HTML 输入文本字段,如下所示:
<input type="text" name="data[testing][name]" id="data[testing][name]">
根据数据库检查了输入文本框字段中的值。
如果该值与数据库中存储的数据匹配,则返回 true。
这是代码:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result){ //doing something;}
}
当我对上面的代码进行一点改动时,我遇到了一个问题,
但随后它就不起作用了:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result===true){ //doing something;}
}
有人可以帮忙吗?
I was doing some self-learning about cakephp (version 1.26).
I got a simple HTML input text field like this:
<input type="text" name="data[testing][name]" id="data[testing][name]">
The value from the Input text box field was checked against the database.
If the value matches the data stored in the database, it will return true.
Here is the code:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result){ //doing something;}
}
I came across a question when I altered the code above with a little change,
but then it failed to work then:
{
$t=$this->data;
$result=$this->User->findByname($t['testing']['name']);
if($result===true){ //doing something;}
}
Could anyone help please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您正在使用 严格 类型比较
===
而不是==
,这意味着$ result
实际上不等于true
,从而使条件失败。尝试看看$result
变量中发生了什么:或者尝试使用 (
==
) 这个条件:或者简单地:
You are using strict type comparison with
===
rather than==
, this implies that$result
is actually not equal totrue
there by making the condition fail. Try to see what does come though in the$result
variable:Or try this condition with (
==
):Or simply:
这里假设 findByName 返回某种对象或数组。如果您使用
if ($result)
这个对象/数组将被转换为布尔值。但是,如果您使用
if ($result === true)
,则您严格将对象/数组与布尔值true
进行比较,此比较将始终计算为 false。Assuming here that findByName returns some kind of object or array. if you use
if ($result)
this object/array will be cast to a boolean.If however you use
if ($result === true)
you're strictly comparing an object/array to the booleantrue
, this comparison will always evaluate to false.PHP 参考对如何进行类型比较有非常好的解释。简单的答案是,您现在正在进行更严格的比较,并且一些边缘情况被遗漏了。您可能会觉得 $result == true
http://php.net 没 问题/manual/en/language.operators.comparison.php
The PHP reference has a very nice explanation of how type comparison is done. The quick answer is that you are now doing a much stricter comparison, and some edge cases are falling through the cracks. You will probably be fine with $result == true
http://php.net/manual/en/language.operators.comparison.php
这是因为
$result === true
检查$result
值是否为true
。但是您的
$result
变量包含来自数据库的结果。That's because
$result === true
checks, if$result
value istrue
.But your
$result
variable contains results from the database.粗略地说,
if
运算符将你的参数转换为布尔值并计算它。因此if($result)
将$result
转换为true
或false
。另一方面,===
实际上检查类型和“值”是否相等,因此true === $val
只会返回true
如果$val
是布尔值true
。===
显然返回一个布尔值,因此if
中的后续计算不需要强制转换。这对您来说意味着,如果$result
转换为true
,if($result)
就会处理该块。变为true
的示例有1
、'1'
和new Object()
。相反,if($result===true)
不会立即转换$result
。它根据布尔值true
检查类型和“值”。如果
$result
是1
,则前一个控制结构将处理该块,但后者不会。Coarsely, the
if
operator casts your argument into a boolean and evaluates it. Soif($result)
converts$result
intotrue
orfalse
. On the other hand,===
actually checks for both type and "value" equality, sotrue === $val
will only returntrue
if$val
is the booleantrue
.===
obviously returns a boolean, so no casting is necessary for the subsequent evaluation withinif
. What this means for you is thatif($result)
processes the block if$result
casts intotrue
. Examples of things that becometrue
are1
,'1'
, andnew Object()
. Conversely,if($result===true)
doesn't immediately cast$result
. It checks it in type and "value" against booleantrue
.If
$result
is, say1
, the former control structure will process the block, but the latter won't.=== 表示等于并且与您等于的类型相同...
但 $result 包含来自 db 的数据..所以它不是布尔值...
使用 == 代替:
=== means equal AND the same type of what you equaling to...
but $result contains data from db..so it's not Boolean...
use == instead:
在您的代码中,当有结果时,返回非零,因此它将计算为
true
。=== 是恒等运算符,当两个对象相同时将返回
true
,例如
1===1 (true) true===true (true ) true===1
(false)== 是相等运算符,当两个对象相等时将返回
true
或同等。例如
1==1 (true) true==true (true) true==1
(true)findByName 将返回一个数组或 <代码>取消设置。
unset
相当于false
,数组相当于true
。值
true
本身永远不会在代码中返回,因此 === 永远不会是true
。In your code, when there is a result, the return is non-zero, therefore it will evaluate to
true
.=== is the identity operator and will return
true
when the two objects are identicale.g.
1===1 (true) true===true (true) true===1
(false)== is the equality operator and will return
true
when the two objects are equal or equivalent.e.g.
1==1 (true) true==true (true) true==1
(true)findByName will return an array or
unset
.unset
will equate tofalse
and an array will equate totrue
.the value
true
itself is never returned in your code, so === will never betrue
.