7

In WordPress' core code, you often see this:

if (1 == $someVar)

as opposed to this:

if ($someVar == 1)

I don't know if the first way is a WordPress-centric style of coding, but I've only noticed it in WP code (either core or 3rd-party code).

20
  • 7
    Yoda conditions these are .
    – fuxia
    Commented Jan 4, 2013 at 23:44
  • 3
    @AKTed It's off topic because it's not specific to WordPress, it's a PHP question. From the FAQ: not specific to WordPress (even if they happen in its context)
    – Milo
    Commented Jan 5, 2013 at 1:28
  • 3
    @Milo: I thought so at first, but it is WordPress specific in the context that WordPress includes it in their Codex as the way they want it done. It does qualify as a PHP question, but my question was specifically about noticing it in WP code a lot.
    – akTed
    Commented Jan 5, 2013 at 2:56
  • 3
    This question is on topic because it pertains to the documented WordPress coding standards. Reopened and answered as such.
    – EAMann
    Commented Jan 7, 2013 at 16:38
  • 3
    Okay, now I'm usually the Scope Police, and this is too pedantic even for me. I would say that a question that dovetails so precisely with the WordPress Coding Standards as this one not only is on-topic for WPSE, but also provides a great opportunity to expound upon the WordPress Coding Standard in question. This was a fastball down the middle; thankfully @EAMann recognized it, and swung for the fences. Commented Jan 8, 2013 at 23:43

1 Answer 1

12

This coding style is known as a Yoda Condition, and it's nothing specific to WordPress. I've used the same style in C++, C#, and even JavaScript code.

The main benefit of a Yoda Condition is that it prevents accidental value assignment from a typo. Consider the following (often-seen-in-the-wild) typo:

if ( $some_variable = 1 ) {
    // ...
}

This will always evaluate to true, but has the additional consequence of assigning the value 1 to $some_variable. Sometimes this is intentional, though. In WordPress specifically, you'll often see things like:

if ( $post = some_function( $_POST['id'] ) ) {
    // ...
} else {
    // ...
}

The point of this code is to assign a new value to the $post variable, and it works because the some_function() being used will return false on error.

Yoda Conditions (reversing the order of the == equality operator) protect you from accidentally assigning values when you didn't mean to. If you make a mistake and only put one = in the code, you'll get an error because you can't assign to a regular value:

if ( 1 = $some_variable ) {
    // ...
}

Like I said, this is not unique to WordPress, or even to PHP, however it is part of the required WordPress coding standards. If you're writing a plugin or theme for general distribution, or contributing to core, you need to know both what Yoda Conditions are and how to use them

1
  • 1
    Thank you for a very descriptive answer, much appreciated.
    – akTed
    Commented Jan 7, 2013 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.