-2

The best way to check the conditions and what is difference between them?

This is my usual way:

if ($this->_is_valid_number() == TRUE) {
    //do some thing...
}

I've seen some code written in this way(for example):

if (TRUE == $this->_is_valid_number()) {
    //do some thing...
}

Are these different from each other? Which method is standard?

Nick
  • 138,499
  • 22
  • 57
  • 95
sina
  • 181
  • 1
  • 12

2 Answers2

4

None of the above, really.

== true is redundant with if(condition) so it could just be written as if ($this->_is_valid_number()) which is pretty standard. If you want to check for false, you would do if (!$this->_is_valid_number()) and if you would check for any other condition, you usually write like you would speak:

If my number is not one -> if($number !== 1)

Notice: Also check this article for difference between == and === operators

MorganFreeFarm
  • 3,811
  • 8
  • 23
  • 46
maio290
  • 6,440
  • 1
  • 21
  • 38
0

The second method has a added benefit of failing if it's used incorrect.
The second one sets true to $number and creates no error, which is what the code should do but not what the coder is expecting.
Do it the other way around and it will fail

$number =5;

if($number == true) echo "true"; // true

if($number = true) echo "true"; //true 

if(true == $number) echo "true"; // true

if(true = $number) echo "true"; // fails

https://3v4l.org/suF9s

Andreas
  • 23,610
  • 6
  • 30
  • 62