PHP Tutorial: Learning PHP (Part 3)

Comment 1 Standard

In Part 2 I covered else-if statements and more complicated if statements. One of the other concepts that’s essential to learning any kind of programming is loops. There are three different types of loops (while loop, for loop, and do while loop), but I’m only going to cover two of them here.

While Loops

The concepts of loops is extremely simple. While some condition is true you’re going to run the same bit of code again and again and again until the condition is no longer true anymore. Right away you can probably see the problem with loops: if the condition never becomes false then the loop with go on endlessly. This is called an infinite loop. Infinite loops should be avoided, they eat up your computer’s resources and can even crash your computer completely. In PHP the worst that will happen is that your browser gets a timeout error or crashes. Not the end of the world, but annoying at the same time.

Usually when I explain this example to people I introduce them to Polly, the brightly colored parrot that looooves to repeat everything you say. So, let’s pretend that we walk up to Polly’s cage with a cracker in our hand. Polly, once she sees a cracker, won’t stop begging for one until you give it to her.

<?php

$treat = “cracker”;

while ($treat = “cracker”)
{

echo “Polly want’s a cracker!<br>”;

$treat = “none”; //you give Polly the cracker after she asked for it so now you don’t have a treat anymore

} //once a loop reaches this part it goes back to the beginning and runs the code inside of these brackets all over again

?>

Run this code and see what it does. Polly asks for the cracker, and because you have a fondness for Polly you decide to give it to her. Now lets say you walk up with 5 crackers. Polly seems them and starts begging again.

<?php

$crackers = 5;

while ($crackers > 0) //as long as you have a cracker
{
echo “Polly wants a cracker!<br>”;

$crackers–; //you give Polly a cracker, so the number of crackers you have goes down by one
}

?>

Now run the script and see what happens. Suddenly Polly is asking you for crackers more then once. In fact, she asks you for a cracker 5 times until you don’t have any more crackers left.

What happens if you take out $crackers–;? Would Polly still ask you for crackers? How many times will she ask you?

The syntax for a loop is really simple. As long as the condition inside of it is true, the loop will go through. Now look at some of these examples –NONE of them work correctly. Can you figure out why?

<?php

$crackers = 0;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
$crackers–;
}

?>

In this case, Polly won’t ask for a cracker because you don’t have any.

<?php

$crackers = 4;

while ($crackers > 5)
{
echo “Polly wants a cracker!<br>”;
$crackers–;
}
?>

In this case, Polly won’t ask you for a cracker because you don’t have more then 5 of them.

<?php

$crackers = 5;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
$crackers++;
}
?>

In this case, Polly will ask for a cracker forever because each time she asks, you suddenly end up with another cracker out of thin air! See how it says $cracker++? That means increase the number of crackers you have by one each time.

<?php

$crackers = 3;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;
}
?>

Can you sport the problem with this one? This will be an infinite loop because you always have 5 crackers and they never decrease as Polly asks for one.

Now, just because this loop looks really simple doesn’t mean loops can’t have other things php syntax inside of them. Take this for example:

<?php

$crackers = 5;

$mood = “friendly”;

while ($crackers > 0)
{
echo “Polly wants a cracker!<br>”;

if ($mood == “friendly”) //you’re in a good mood today
{
$crackers–; //you give Polly a cracker
}
else
{
echo “Sorry Polly, no crackers for you today!”;
$crackers = 0; //you eat all your crackers yourself
}

}
?>

In this case you only give Polly your crackers if you’re in a friendly mood. Try it out and see what happens when your mood changes.

Programming Challenge! Lets say one day you walk up to Polly and you have 5 crackers. You’re in a good mood so you decide to give her one. Unfortunately Polly isn’t in such a good mood and she bites your finger after the first cracker you give her. Once that happens your mood changes to angry and you eat 2 of the crackers yourself. But Polly is persistent and after a change of heart you give her the remaining 2 crackers. Can you write a while loop to reflect this?

For Loops

Another type of loop is called a for loop. Usually you use a for loop when you have a set amount of times you want to repeat a portion of code. For instance, lets say you run a pet shop and you know you have 23 cages needing cleaning every day. In this case we know you’ll always have to clean a cage 23 times. These are the cases where its appropriate to use a for loop. When you’re uncertain how often you’ll need to repeat a portion of code its more appropriate to use a while loop. Now lets take a look:

<?php

for ($cleancages= 1; $cleancages < 23; $cleancages++)
{
echo “I cleaned cage number $cleancages<br>”;
}

?>

Try running this script. You’ll see that you cleaned cages 1-23. A for loop works exactly like a while loop with a little difference.

$cleancages = 1;

The variable above, at the beginning of the for loop, sets $cleancages to equal one.

$cleancages < 23

Here the for loop checks to see if the number of clean cages is less then 23. As soon as clean cages goes to 24 the for loop stops, because there are only 23 cages and 24 is greater then 23. This part of the for loop is exactly the same as what you would put inside of a while loop.

$cleancages++

One thing to notice is that there is NO SEMICOLON at the end of this. That is how the for loop knows you want to loop for a certain number of times. This part of the for loop is the same as when we were doing $crackers++; or $crackers–; It tells the for loop that ever time it runs it should increase the variable $cleancages by a value of 1.

So, there you have it. Think you can do this?

Programming Challenge! Write a script that has two variables, $forloop and $whileloop. Make it so that if there is a value in the $forloop variable that the script will run a for loop to clean a number of cages. However, if there is a value in the $whileloop variable the script should run a while loop to clean the cages instead. Once you have that done, see if you can make it so the script will run both a for loop and a while loop if there is a value in either of the two variables.

All done? Now try your hand at Pits of Doom — a free php/mysql online game tutorial that will walk you through designing your own database driven game from start to finish.

Advertisements

One thought on “PHP Tutorial: Learning PHP (Part 3)

  1. Pingback: Learning PHP (Part 2) « Online Game Development

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s