Please Note: You may have disabled JavaScript and/or CSS. Although this news content will be accessible, certain functionality is unavailable.

  • ebooks

    Your E-book is your traveling website, send it out in style with Sirius

    + read more

  • graphics

    Business cards, brochures, newsletters, ads and more. You provide the concept, we make it happen.

    + read more

  • web design

    At Sirius "good enough" doesn't cut it. Not for us, and definitely not for our clients. We'll give you a high-end look without breaking the bank.

    + read more

  • writing

    Can't find the perfect words to get your message across? Relax. Our Writing Team has you covered.

    + read more

PHP: Getting Loopy

Written by Allison Day - February 25, 2010 12 Comments
 
Hangman
Photo courtesy of Phu Son Nguyen

Have you ever played hangman? I used to, all the time. Especially when I was bored, and didn’t have a book to read or anything else to do. This was before the 24/7 connection of computers and iPhones, of course.

If you haven’t, let me explain: hangman is a simple little word game. One person thinks up a word, then writes out dashes and spaces to represent the number of letters in the word. If my word was ‘sushi’, I’d write: _ _ _ _ _. The other person’s job is to guess the word, letter by letter. For every letter they guess that isn’t part of the word, a part of the hangman is drawn – until either the hangman is completed and they lose, or they guess the entire word and win.

But what does this have to do with PHP code… ?

So You Want To Write A Program…

Say you wanted to write a PHP program, so you could play hangman on your computer. How would you do it?

If you’ve been reading my coding series, your first thought might be, “If statements!” Okay, so let’s see how your pseudo-code might look if you used a bunch of if-statements to write the program: if (hangman isn't finished) {
if (word isn't finished) {
if(the first letter in the word matches the guess) {
display the letter
indicate that there's been a match

}
if(the second letter in the word matches the guess) {
display the letter
indicate that there's been a match

}
if(the third letter in the word matches the guess) {
display the letter
indicate that there's been a match

}
if(the fourth letter in the word matches the guess) {
display the letter
indicate that there's been a match

}
if(the fifth letter in the word matches the guess) {
display the letter
indicate that there's been a match

}
if (there isn't a match) {
add a part to the hangman
}

}

}
And that’s only for one guess, for a word that’s only five letters long. What if you had a 20-letter phrase? Or 15 guesses?

Yuck. As you can see, it gets real ugly, real quick.

So What’s A Programmer To Do?

Lucky for us, there are more tools in our coding toolbox than just the if/else statement. (Can you imagine if you had to build a house with just a hammer? Darn near impossible.) So how would I code something like this?choose the word to be guessed;
set the hangman to empty;

while (the word is incomplete and the hangman is incomplete) {
tell the person the game is still going;
match = false;
guess = the person's guess;
for (each letter in the word) {
if (letter is the same as $guess) {
match = true;
remember which position this letter goes in;

}

}

if (match == true) {
display positions where the letter is the same as the guess;
} else {
add a part to the hangman;
}

}
You see, we also have things like while statements, for statements, and if I wrote it out as real code you’d even see a couple of foreach statements in there.

Okay. Time to explain.

Loops

Remember our if-statement last week? Each line in the entire statement would be read by your browser (or parser) once, and then when the parser was done with the statement, it would move on to whatever code came next.

That’s not how loops work.

Loops are, as the name suggests, sections of your code that the parser loops through several times. There are several different types of loops – some of which are the while, for, and foreach loops. (There’s also a do-while loop, but you’re a lot less likely to see it in most code. It’s almost exactly like the while loop, except… backwards. In the do-while loop the condition test is done at the end of the code block, instead of at the beginning like in a while loop.)

Those are what make it so you can write cleaner code, like my pseudo-code, instead of those messy repetitive if-statements.

You Can’t Just Leave Us Hanging Like This…

Yes, I can. Technically, I’ve given you all the resources you need to figure out on your own what all these loops do. Go ahead, click the links. You’re smart enough to figure it out, I know you are.

But if you prefer my explanation style to that of the authors at PHP.net? (You’re a sweetheart, really you are.) Then come back the next couple of weeks, when I go into detail about the different types of loops, and show you some real code that would actually create the hangman game for you.

And if you’re not sure if you prefer my writing style to theirs? Well… I’ve never claimed to be above bribing with homemade cookies…

Read the Comments

12 Outstanding Responses to "PHP: Getting Loopy"

    Martin on February 25, 2010 at 11:00 am | Permalink

    guess = Sirius Graphix

    if (match==true) {
    Start new hangman

    _ _ _ _ _ _ – _ _ _ _

    }
    else {
    add a part to the hangman;
    }

     

    Allison Day on February 25, 2010 at 12:44 pm | Permalink

    You rock, Martin. ^_^

    Yep, you got it right. Hm, let me guess… ‘A’? ‘S’? ‘T’? :D

     

    Davina on February 25, 2010 at 12:54 pm | Permalink

    I feel a little loopy after reading this! This whole coding thing boggles my mind and I’m in awe of people such as yourself who do this. I’m almost fascinated enough to try to learn; if I had the time I would. As for hangman… used to play it all the time. Give me a word game and I’m happy.
    Davina’s last blog post… Getting Sensual with Creativity

     

    Martin on February 25, 2010 at 1:31 pm | Permalink

    Yeah :-D

    What, 3 letters at once?

    Ok, because it’s you:

    _ S _ _ _ _ – _ _ _ _

    / \

     

    Allison Day on February 25, 2010 at 1:42 pm | Permalink

    Davina – Thanks… hopefully the way I break things down, at least a little bit of it will rub off on you! I know it looks all complicated and whatnot, but it’s really not so bad. :)

    Martin – Hmmm… ‘O’? :D

     

    Martin on February 25, 2010 at 1:54 pm | Permalink

    Now we are getting nearer: :)

    _ S _ _ _ O – _ O _ _

    / \

     

    Allison Day on February 25, 2010 at 2:25 pm | Permalink

    ‘D’? :D

     

    Martin on February 25, 2010 at 2:50 pm | Permalink

    I think you know the answer :)

    _ S _ _ D O – _ O D _

    / \

     

    Allison Day on February 25, 2010 at 3:15 pm | Permalink

    I think I do. “Pseudo-code”, right? :D

     

    Martin on February 26, 2010 at 2:51 am | Permalink

    Yes, Pseudo-code is right.

    You are :)

     

    maquis on February 26, 2010 at 1:41 pm | Permalink

    mmmmm… tasty loops.

    Allison, I envy your ability to make coding approachable. Do you run it past a non-coder before posting it to make sure that you’re making sense? My biggest problem is that I forget to explain some parts of it…

     

    Allison Day on February 26, 2010 at 6:31 pm | Permalink

    Thanks, Maquis. :)

    Sometimes. If I get the post finished early enough, I ask Rose to edit it and read it for understandability, and Wendi and Deb too if they have the time. Other times, when I get the post finished a few hours before I publish it (like this one… oops!), I just write it, then cross my fingers and hope it makes enough sense to non-coders. :)

     

Start a Conversation!

... and if you want a gravatar, grab one here!

Your email is never shared. Required fields are marked *

Spread da Luv!

Subscribe without commenting