codeswan blog

PHP: Getting Loopy

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 is not finished) {
if (word is not finished) {
if(the first letter in the word matches the guess) {
display the letter
indicate that there is a match
}
if (the second letter in the word matches the guess) {
display the letter
indicate that there is a match
}
if (the third letter in the word matches the guess) {
display the letter
indicate that there is a match
}
if (the fourth letter in the word matches the guess) {
display the letter
indicate that there is a match
}
if (the fifth letter in the word matches the guess) {
display the letter
indicate that there is a match
}
if (there is not 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 persons 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…

2 Responses to PHP: Getting Loopy

I stumbled upon your blog while searching for ways to understand and complete my hangman in php. I’m glad I found your blog it’s quite helpful! however I can’t seem to be able to access the hangman code you provided on the “PHP: Real Code, And A Game Of Hangman” with the comments etc (when i click on the link to download it tells me access denied) I was wondering if there was any way for me to see the code? thanks in advance !

Leave a Reply

Your email address will not be published. Required fields are marked *