If, Then, Else

August 9th, 2008

Ever become brave enough to look inside of a WordPress php file? Specifically, a php file from a theme? If you have, you may have noticed that they are filled with If, Then, and Else statements. If you have no idea what these mean, this post is for you.

Php has always been a pain for me to understand, but if you think about it in a logical manner, it begins to make sense. If, then and else statements are used as a means of making decisions, similar to the way you and I make decisions in the real world. Here is an example of how this logic works in WordPress.

< ?php if (have_posts()) : ?>

The if statement within that php function is asking if there are posts. If the blog has posts, WordPress begins processing what is known as “The Loop“.

<? php while (have_posts()) : the_post(); ?>

This piece of code tells WordPress to grab the posts from the MySQL database and display them on your blog. The loop then closes with this function.

<?php endwhile; ?>

If the answer to the if question turns out to be no, WordPress bypasses the loop and displays a message that no posts exist. This is where the else statement comes into play.

< h2 class=”center”>Not Found< /h2>
< p class=”center”>Sorry, but you are looking for something that isn’t here.< /p>
< ?php include (TEMPLATEPATH . “/searchform.php”); ?>
< ?php endif; ?>

That last line of code is the if statement and concludes the function.

If, then, and else are easy to remember when you think of how you function on a daily basis. A real world example, (If I am hungry, Then I’ll eat some food, or Else I’ll starve). Maybe not the best example in the world, but you should be able to get the picture.

I’m no coder by any means but when I read explanations about statements and functions inside of WordPress via Lisa Sabin Wilson’s WordPress For Dummies, it’s as if light bulbs turn on inside of my head. One of those “Ohhhh, so thats what that does” sort of moments.

Be sure to let me know if this post was of any help to you.




  1. Frederick (4 comments.) says:

    Maybe a better version of your example is:

    If I am hungry, then I’ll eat some food, Else (since I am not hungry) I will not eat.

    • Jeffro2pt0 (164 comments.) says:

      I’ll think about changing the example to the one you provided. Thanks for that!

      • Robert A. Henru (1 comments.) says:

        Yup Jeff, you better change the example. It’s kind of misguiding the real meaning of the if then else.

  2. Jacob Santos says:

    The conclusions of our decisions may look boolean, but a lot of our decisions we don’t actually ponder out in full detail and let our subconscious do the heavy lifting. Boolean logic is far easier to comprehend and implement than fuzzy logic, which PHP doesn’t have internal support for.

    Most people don’t break the logic down to IF I don’t eat THEN I starve, unless they are explaining why they do eat on constant basis to a being that doesn’t have to eat. They consider, “What am I going to eat when I’m hungry?” Should I eat rice, go out to a restaurant, or frozen meal? The decision is further broken down.

    Sure you can probably get away by breaking the fuzzy logic down to its Boolean decision based on the Set mathematics that goes into fuzzy logic, but the idea is still the same.

  3. Carson (46 comments.) says:

    In the interest of splitting hairs, there is no “Then” syntax in the language. The “Then” is what you do when a condition is met. I would describe it as an “if, elseif, else” construct.

    • Jacob Santos says:

      The “Then” is the “:” or “{“, this isn’t BASIC.

      • Carson (46 comments.) says:

        Now you’re splitting hairs. Anyway, if the “:” or “{” are the “Then”, it pretty much is Basic. I’ll stand by my hair split; the author didn’t call it “if, :, else.” Also, using “if, then” is considered bad grammar.

        • Jeffro2pt0 (164 comments.) says:

          Grammar and what have you put aside, I wrote this post as a way to explain just a little bit about how PHP or WordPress works. It looks like I have failed.

          I don’t care what any of you coders say about this or that. The way this was presented in the book just happened to be the way in which I finally understood what those functions and statements meant. I figured someone else would benefit from this particular explanation.

          • Carson (46 comments.) says:

            Hey, I did open by admitting that I was splitting hairs. Don’t let a nitpicker like me discourage you. I’m sure your work will benefit many of your readers. Press on!

        • Jacob Santos says:

          You have me there, I don’t know much about grammar, PHP grammar maybe, but not English grammar.

      • Aaron Brazell (15 comments.) says:

        Jeff is right in this situation. The “then” is the colon. It comes from Bash and probably other languages where then is the continuation if a condition is met.

        if( I am hungry) then (I’ll eat)
        else (I will just go play video games until I do get hungry)

  4. Robert (3 comments.) says:

    It was about time that someone took the charge to teach programming basics as they are met in themes to users – the more eductated users become, the lesser chances are that villain theme authors impose security threats onto them (same for plugins, btw). I’d suggest to expand an this and try to break down switch() as a next lesson.

  5. Rajesh (35 comments.) says:

    If you do not know programming
    ( read this article;
    } else {
    skip it;

    Guess fuzzy logic is getting too confused here…

  6. Neil (30 comments.) says:

    When i was a young kid we used to go over lines of pascal code, and funny enough it has a similiar structure to it like php, in the sense that every code is logical and in a way i think its a bit like slang. Code unfortunatley if not in the right place or missing any character will not work unlike normal languages. As easy as the code can be to understand, it needs to be learnt fluently before you can really use it and get good results.

  7. Lisa (8 comments.) says:

    Man, these guys are more pedantic than my editors haha :)

    Jeff, I’m very happy to know that my simplified explainations helped spark the bulb :D

    • Jeff Chandler (295 comments.) says:

      Hey Lisa. WordPress for Dummies has sparked numerous bulbs. I loved the way to presented what I thought was complicated information into easy to understand lingo.

  8. Monika (40 comments.) says:

    I would like to cook bakon eggs.
    If I found eggs in the refrigerator than I can cook now,

    if not have to go to the market.

    I would like to have a very expensive car.
    If I have enough money I could buy it this days. If not, I have to wait or I have to ask for a credit.

    This are my examples I try to explain if else.


    • Brad - Personal Blog Tips And Blog Help (1 comments.) says:

      What in blazes does that mean? ;)

  9. amolpatil2k says:

    Because the world generalizes us into stereotypes, we generalize the world in return. For instance this posts talks to ALL (programmers and non-programmers) in the same way. The fact that so much conversation is recorded means that so much stuff is non-interactive so that the two parties can never really find a common plane for the conversation. On top of all this, the system is so demanding of people that we mostly can’t do what we really want. The stereotyping and the stress of competition finally takes its toll and we become zombies. Linkin Park’s Numb comes to mind.

  10. Ross Calloway (1 comments.) says:

    Let no good deed go unpunished, or something like that. Your attempt at explaining php lingo is greatly appreciated. WordPress for dummies is one thing. Php for dummies would be good too.

    Unfortunately for me, I let programming pass me by back in the early 90′s when, it seems, so many new languages came up that I was overwhelmed to keep up and still keep a full-time job. Basic, Pascal, Turbo Pascal, then C finally put me over the edge.

    I understand the if,then,else logic, its the code that says that that I have problems with. Now, I’m just as happy to pay someone for a code fix or a theme that I like instead of pulling out the rest of my hair.

  11. Ray (6 comments.) says:

    Your post is right on and very informative to the non-programming portion of the folks reading here. It may be a bit informative to some of us that are programmers and not that familiar with PHP. For that are are saying If … Then is not a good grammar construct, keep in mind PHP (and all programming languages for that matter) have their own syntax (or rules of grammar) that do not necessarily correspond with those of the grammar rules of modern English. The If … Then instead of If … : makes sense, as a basic (not BASIC) construct of most all programming languages include some kind of If situation X is TRUE then do Y, and historically more often then not the phrasing is If … Then, though some are more cryptic then others and may use symbolism instead of saying what they really mean. Interestingly though in the older versions of Fortran the IF evaluates the expression as negative, zero, or positive and jumps to a respective marker in the code. Anyway, that is probably going to to deep…

  12. Chris Carvache (3 comments.) says:

    Interesting. To be honest I’m more interested in the fact that there’s actually a programming article on here.

    No offense but I don’t think the whole syntax of the if then else should be a topic of concern as almost every tutorial I’ve seen online explains the concept in this way. No, its not grammatically correct but, in this field, the jargon is indeed correct and is used appropriately.

    I’d like to see more articles like this that expand on the whole programming aspect especially since WordPress is one of the simplest platforms to develop on.

    I can only imagine what people would say once arrays and the wp_query object are mentioned!

  13. Velvet Blues (1 comments.) says:

    Somehow, I don’t think this is sufficient for the ‘Dummies’ that you might be trying to reach… which is why, I guess, you are linking to Amazon.

    Perhaps you should have an article entitled:

  14. Chris Carvache (3 comments.) says:

    Entitled what?

  15. esa (1 comments.) says:

    well..thanx for ur share. btw, how can i made the comments like that? reply etc :)

  16. Skippern (4 comments.) says:

    It is better to compare it with a shopping list:

    IF we need sugar; buy sugar; ELSE goto next item
    WHILE need more potatoes; pick potato into sack; ENDWHILE;

    You can continue like that

    • Chris Carvache (3 comments.) says:

      That’s a good idea. You can even at that point start introducing multiple loops into the equation kind of like…

      While IAmAlive :
      If Weekday:
      While AtWork:



      My Life in a nutshell :)

  17. bubazoo (213 comments.) says:

    oh yeah most definitely! in fact! I didn’t know the
    “TEMPLATEPATH” variable existed in wordpress, so I actually learned something new in that post :) I’m actually better at understanding PHP then I am wordpress functions and statements themselves, mostly because of the lack of wordpress’s explination of all that. The codex is so confusing, and whenever you try to ask for help in the WordPress forums, they treat you like your stupid because your not a scientific mathematician major like the rest of the community acts like they are, putting down newbies as if we’re stupid just for asking a question. what I mean is, a post like that would get you thrown off the wordpress forums for being too much information that you couldn’t find in the codex. I don’t know why they act like that, and in IRC its worse…like not all of us wordpress users are professional web site developers with a masters in mathmatician guys, lordy. your treated as if your “beneath” them so to speak like all cocky and stuck up and stuff.


  1. [...] you are new to conditional statements, you can read one of our earlier post about if, then and else conditions. (No Ratings Yet)  Loading … Sphere: Related Content [...]

  2. [...] If you are new to conditional statements, you can read one of our earlier post about if, then and else conditions. [...]

Obviously Powered by WordPress. © 2003-2013

page counter