# Best practice if statment [closed]

Which is good to use and why?

if ($a == 3) { //do something }  Or  if (3 ==$a) {
//do something
}

• Id say this isn't a question for code review, as there isn't any code to review ! – Dan Jul 5 '18 at 20:24
• This isn't a code review, but it is a interesting question. The last snippet is called Yoda notation. The Symfony Coding Standards require Yoda notation, but PSR, Slevomat and Squiz don't. – Stephan Vierkant Jul 5 '18 at 20:58
• @Stephan and OP: It's considered a bad practice in most other languages. Relevant Software Engineering answer. – Mast Jul 5 '18 at 21:03
• I agree. One of the few rules in the Symfony Coding Standard I don't agree with. – Stephan Vierkant Jul 5 '18 at 21:10

Neither and both.

The 'both'

x == $assignedVariable is a method of defensive programming which is aimed at preventing accidental assignment if someone was to use $x = 3 by mistake. Whether this offers value is debatable; modern systems exist which catch these mistakes, IDE' can flag this easily and its a bit moot given that someone can accidentally do $x = 3 just as easily as 3 =$x. The difference is assignment vs exception.

The 'neither'

You are using a truthy condition(==) which in your example should be changed to a value and type check(===). Truthy checks absolutely still do have a place but within the confines of your example a type check is definitely suitable. Take the following example which should highlight the point.

var_dump($a == 3); // true var_dump($a === 3); // true
var_dump($a == "3"); // true var_dump($a === "3"); // false