The WHILE loop is one of the most useful commands in PHP. It is also quite easy to set up and use – granted it might look a bit complicated. First off however let’s figure out just what the hell a WHILE loop is – and what we need it for. A WHILE loop will, as the name suggests, execute a piece of code while a certain condition is met. So – what’s the point? WHILE loops make repetitive tasks much less annoying than they usually are. Let’s take a look at a few simple examples.

Making Bart Simpsons Life Easier

Let’s say you have a specific bit of code that you need to repeat several times. You can sit there and manually type everything out; or you can use a WHILE loop. I’m sure most of us here have watched the Simpsons at some point or another right? Remember how Bart is always writing something over and over again on a blackboard in the opening of each episode? Let’s use that as an example. Assume we need to write “I will not pledge allegiance to Bart.” 50 times on our website. We can utilize the WHILE loop to ECHO that specific text 50 times:

$times = 50; 
$x = 0; 
while ($x < $times) { 
echo "I will not pledge allegiance to Bart. "; 
++$x; 
}

So, first we set the $times variable which holds the number of times we want to repeat our text. Then we set the $x variable which is the one that will count the number of times the code has been executed. After these is where the WHILE loop comes into play. On line 3 we tell the server to repeat the code while $x is less than $times. This is followed by the code to be executed which is enclosed in { }.

After the echo line which prints out the text, there is another very important line:

++$x;

What this does is exactly the same as writing:

$x = $x + 1;

It adds one to the value of $x. This code is then repeated (as $x now equals 1). It continues being repeated until $x equals 50 (the value of times) at which point the while loop will stop. Now if we could just figure out how to get Bart a digital blackboard - he can start programming WHILE loops in PHP instead of wasting all that chalk.

bart-simpson-while-loops-php-rapid-purple

Using $x To Do More

The variable counting the number of repeats ($x in the above example) can be used for much more than just counting the amount of times a WHILE loop has been ran. Let's take another example. Let's say we wanted to list all of the even numbers between 0 and 100, not including 0 but including 100.

Once again we're going to start off by setting up our variables.

$number = 100;
$startingNumber = 0;

Now let's make up our while loop. We know that if we start with 0 and count by 2 all the way to 100 we will get all of the even numbers between 0 and 100. So let's write out our loop which will add 2 to our $current variable until it's equal to 100.

$number = 1000; 
$startingNumber = 0; 
while ($startingNumber < $number) { 
$startingNumber = $startingNumber+2; 
echo "$startingNumber, "; 
}

There are a few key things I want to point out here. Firstly, you will notice that I have placed the $startingNumber = $startingNumber+2; line before the echo statement. If the echo statement runs first it would echo out our initial value for $startingNumber which we set at 0 - and we don't want to do that.

The other reason for this is that, if that line was after the echo line, the loop would also stop before it reached 100 - thus displaying 98 as the last even number. This happens because the addition to $startingNumber occurs AFTER it has been displayed. So the script runs, gets to 98, displays 98, adds 2 to 98 to make 100 - then goes back to run the loop however 100 is not less than 100 - so the loop stops. If you wanted to display both 0 and 100 we would change our WHILE loops condition to

$startingNumber <= $number

Published by Michael Boguslavskiy

Michael Boguslavskiy is a full-stack developer & online presence consultant based out of New York City. He's been offering freelance marketing & development services for over a decade. He currently manages Rapid Purple - and online webmaster resources center; and Media Explode - a full service marketing agency.

Leave a comment

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