Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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).

share|improve this question
7  
Yoda conditions these are . –  toscho Jan 4 '13 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 Jan 5 '13 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 Jan 5 '13 at 2:56
3  
This question is on topic because it pertains to the documented WordPress coding standards. Reopened and answered as such. –  EAMann Jan 7 '13 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. –  Chip Bennett Jan 8 '13 at 23:43
show 15 more comments

1 Answer 1

up vote 10 down vote accepted

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

share|improve this answer
1  
Thank you for a very descriptive answer, much appreciated. –  akTed Jan 7 '13 at 16:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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