Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

if(something) vs if(something===true)

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?

like image 309
user327712 Avatar asked May 24 '26 11:05

user327712


1 Answers

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;}
like image 116
Sarfraz Avatar answered May 26 '26 20:05

Sarfraz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!