Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Why using Yoda conditions you should probably not be

Grégoire Paris wrote down his opinion on why he dislikes Yoda conditions.

So how do Yoda conditions work? Well it is basically a knee-jerk reaction you have to develop: whenever you write a condition, put the operand that cannot be assigned on the left. This should give you an error message if you make an assignment when you actually meant to make a comparison.


Personally, I’m not a big fan of Yoda conditions either. My feeling is the the cost of decreased readability is just too high for the small value that Yoda conditions bring to the table.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.
  • David J Eddy

    In general I agree; yoda’s are mentally taxing to read. That being said there is some instances where (PHP specifically) will not allow you to compare the result of a function call inside the comparison operation; it will however, all you to do the comparison if the non assignable operand is first.

    – `if (!isset(some_fn($array))) {…}` results in the error of ‘Fatal error: Cannot use isset() on the result of a function cal’.

    – `if (null !== isset(some_fn($array))) {…}` does work however.

    Not saying this is a recommended way of doing a comparison operation (I personally am not a fan of nested operation in a comparator); simply stating the possible use case.