In reviewing PHP coding standards for WordPress, best practices include “Yoda” code for logical comparisons. What does this mean?

Simply put, this means to always put the variable on the right side as in this example:

1
2
3
if ( true == $the_force ) {
    $victorious = you_will( $be );
}

According to the documentation:

“In the above example, if you omit an equals sign (admit it, it happens even to the most seasoned of us), you’ll get a parse error, because you can’t assign to a constant like true. If the statement were the other way around ( $the_force = true ), the assignment would be perfectly valid, returning 1, causing the if statement to evaluate to true, and you could be chasing that bug for a while.”

Here is a real world example I encountered in a theme:

The footer area in the site https://www.pacestaffing.com/ has several social media links.
Pace Staffing Social Media LInks

The site owner noticed that all of the links had hash tags (#) instead of the actual URL. The links had been properly entered into the site in the content area for social media links. In investigating the code, I discovered this:

1
if ( $link = "#" )

In the above example, the variable $link gets a “#” assigned to it causing the problem. If instead, the theme developer had coded:

1
if ( "#"  = $link )

there would have been an error, preventing this bug from being introduced into the code.