Not that confusing, it is.

Back end web programming is all about logic. You create a bunch of conditions in which you want your code to run and then you run it. For instance, you would say “If this post has the category ‘featured’ checked off, display it. If not, do not display it.”. That is one of the most common pieces of logic you will encounter in all types of computer programming.

So what in the world is Yoda Logic?

It pretty much is what it sounds like it might be. It’s basically the method for building your logic that, when said aloud, sounds like yoda. Check out this example:

The first part of your statement is the constant, which is the term  “true” and the second part of your statement is the variable, which is the force. Truth always exists but the force may or may not, in this example. (deep stuff!)  It’s called Yoda Logic because our normal language would have you say “if the force is true, you will be victorious” but that’s not the order in which it’s actually written in the code. If you actually read the code aloud it sounds like “If true is the force, victorious you will be”. Sounds like yoda, right?! BAM! Lesson learned.

Mostly.

Notice how we used ‘==’ instead of just ‘=’? We do this because there are really two types of ways you can equate things to each other. You can tell the code “This equals that” in the same way you would assign a variable to some content. Like this:  \$the_force = 'a power inside of you' . This code allows you to use the variable \$the_force anywhere you might want to say “the power inside of you”. This works because it’s a shorter and more concise way of saying it. Basically, you just assigned a variable using ‘=’.

So when is the ‘==’ sign used? It’s used when you’re comparing two things. You’re checking to see if they’re equal, you’re not proclaiming them to be equal. You use the ‘==’ inside of if statements such as the one listed above.

Yoda logic is basically meant to save you from your own mistakes.

Checkout what WordPress has to say on Yoda Conditions