Reputation: 3321
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?
Upvotes: 2
Views: 2135
Reputation: 6571
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
.
Upvotes: 1
Reputation: 4212
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.
Upvotes: 3
Reputation: 382806
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;}
Upvotes: 7
Reputation:
=== 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)
Upvotes: 1
Reputation: 18014
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.
Upvotes: 1
Reputation: 32542
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
Upvotes: 2
Reputation:
That's because $result === true
checks, if $result
value is true
.
But your $result
variable contains results from the database.
Upvotes: 1