This is an old post from 2013. As such, it might not be relevant anymore.
Everyone’s done it, they’ve missed the second equals sign in an if-statement and created an assignment instead. Much tutting, shaking of the head, and a little chuckle ensues as you realise your error. There is a thought amongst some that Yoda notation is the way to go, so named because it switches the comparative’s to produce errors instead of unexpected behaviour. Consider the following:
if ($theSkyColour = 'blue') { // This will always be called, because the value will be "returned" // Equivalent of if ('blue') { }
If you would switch the comparatives around you would end up with:
if ('blue' = $theSkyColour) { // I will produce an error // PHP Parse error: syntax error, unexpected '=' in file.php }
So you immediately know there is something wrong and you can correct it before it lands in production. But is this the best way to go about it? Possibly, it’s a matter of opinion; it’s been around for years and has made its way into some of the largest PHP projects such as WordPress, Symfony, etc.
if ($foo = $bar) { // I am always run and no errors are ever thrown }