Quantcast
Channel: User andrewsi - Stack Overflow
Viewing all articles
Browse latest Browse all 51

Answer by andrewsi for Why do Wordpress code consistently have the uncommon "boolean literal === some dynamic expression" order of comparison parts?

$
0
0

There's an explanation on the WordPress site:

https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#yoda-conditions

It's called Yoda conditions; in short, it's because it's very easy for someone to mix up the assignment operator in PHP with the equality operator, so given a line of code:

if (x = true) 

is almost certainly a typo, but won't generate any errors, because it's valid syntax.

On the other hand:

if (true = x) {

means that you're trying to assign the value of x to the constant true, and PHP will see that and generate an error message.

So it's an easy way to avoid some of the subtle syntax issues that PHP lets you get away with.


Viewing all articles
Browse latest Browse all 51

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>