CIRCLE CIRCLE - 18 days ago 6
Git Question

Non-strict equality comparisons

Today I was exploring the symfony repo and found some pull requests by @Seldaek where he switched the variable with the value as you can see bellow:

enter image description here

Link for the PR

What's the difference between having

$var === true
and
true === $var
?

Answer

There is no functional difference, it's a coding style called yoda conditions.

People use it to avoid accidental assignments in conditions which normally wouldn't be picked up by the compiler when the constant is second.

This is a valid statement, and will be difficult to debug when actually expected it to perform a comparison:

if ($var = 12) {}

This is an invalid statement and will throw an error

if (12 = $var) {}

as we cannot assign the variable to 12.

Comments