if(某事) vs if(某事===true)

发布于 2024-09-07 00:13:33 字数 628 浏览 4 评论 0原文

我正在自学 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 技术交流群。

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

发布评论

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

评论(7

肩上的翅膀 2024-09-14 00:13:33

您正在使用 严格 类型比较 === 而不是 ==,这意味着 $ result 实际上不等于 true ,从而使条件失败。尝试看看 $result 变量中发生了什么:

var_dump($result);

或者尝试使用 (==) 这个条件:

if($result == true){ //doing something;}

或者简单地:

if ($this->User->findByname($t['testing']['name'])){ //doing something;}

You are using strict type comparison with === rather than ==, this implies that $result is actually not equal to true there by making the condition fail. Try to see what does come though in the $result variable:

var_dump($result);

Or try this condition with (==):

if($result == true){ //doing something;}

Or simply:

if ($this->User->findByname($t['testing']['name'])){ //doing something;}
神回复 2024-09-14 00:13:33

这里假设 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 boolean true, this comparison will always evaluate to false.

若无相欠,怎会相见 2024-09-14 00:13:33

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

屋檐 2024-09-14 00:13:33

这是因为 $result === true 检查 $result 值是否为 true
但是您的 $result 变量包含来自数据库的结果。

That's because $result === true checks, if $result value is true.
But your $result variable contains results from the database.

挽容 2024-09-14 00:13:33

粗略地说,if运算符你的参数转换为布尔值并计算它。因此 if($result)$result 转换为 truefalse。另一方面,=== 实际上检查类型和“值”是否相等,因此 true === $val 只会返回 true 如果 $val 是布尔值 true=== 显然返回一个布尔值,因此 if 中的后续计算不需要强制转换。这对您来说意味着,如果 $result 转换为 trueif($result) 就会处理该块。变为 true 的示例有 1'1'new Object()。相反,if($result===true) 不会立即转换 $result。它根据布尔值 true 检查类型和“值”。

如果$result1,则前一个控制结构将处理该块,但后者不会。

Coarsely, the if operator casts your argument into a boolean and evaluates it. So if($result) converts $result into true or false. On the other hand, === actually checks for both type and "value" equality, so true === $val will only return true if $val is the boolean true. === obviously returns a boolean, so no casting is necessary for the subsequent evaluation within if. What this means for you is that if($result) processes the block if $result casts into true. Examples of things that become true are 1, '1', and new Object(). Conversely, if($result===true) doesn't immediately cast $result. It checks it in type and "value" against boolean true.

If $result is, say 1, the former control structure will process the block, but the latter won't.

青春如此纠结 2024-09-14 00:13:33

=== 表示等于并且与您等于的类型相同...
但 $result 包含来自 db 的数据..所以它不是布尔值...
使用 == 代替:

if($result==true)

=== means equal AND the same type of what you equaling to...
but $result contains data from db..so it's not Boolean...
use == instead:

if($result==true)
So要识趣 2024-09-14 00:13:33

在您的代码中,当有结果时,返回非零,因此它将计算为 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 identical

e.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 to false and an array will equate to true.

the value true itself is never returned in your code, so === will never be true.

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