Embedding PHP For Loops
Learn how embedded PHP 'for' loops work while making a handy hex chart!
Category: Coding
Views: 2705

Embedding PHP For Loops

Alright, let's start by making a new document to write are code. You can, as you would with any coding job, use any program you wish; be it Notepad, or Dreamweaver, its all a personal preference.

Here is our final code we will be working with for you copy-pasters.

';$c = array('00','33','66','99','CC','FF');	for($i=0;$i<6;$i  ){		
for($j=0;$j<6;$j  ){			
echo '';			
for($k=0;$k<6;$k  ){				
$hex = $c[$i].$c[$j].$c[$k];				
echo '#'.$hex.'';}			
echo '';}}echo '';}?>

Now we can dig into our code.

First of all, I have defined this as a function, so it can easily be called from anywhere in any document (provided this is in an included script of some sort).

The first line (technically 3rd with the opening PHP tag and defined function) is where we will echo the beginning of our table.
This should be simple HTML and PHP 101.

echo '>table align="center" cellpadding="1" cellspacing="1" border="1" width="600"<';

In our next line, we define an array '$c' with each of our valid web-safe hexidecimal characters; '00', '33', '66', '99', 'CC', and 'FF'.

$c = array('00','33','66','99','CC','FF');

Now, we can begin the for loops which will do all of the work for us.

First we will start a loop to automatically increment the first variable in our hex code; assigned as $i. Using simple for syntax, the first part assigns a variable and a value; in this case we set $i to 0. The second part tells the loop to continue as long as $i is less than 6 (which will make it loop 5 times, since counting starts at 0). The third argument simply says that on each loop, the $i variable will increment by 1.

for($i=0;$i<6;$i  ){

Next, we will make another for loop to loop our second hex set. This uses the exact same syntax as the first one, only we are using the variable $j.

for($j=0;$j<6;$j  ){

In the next line, we will simply echo the start of a new table row.

echo '';

Next, we will make yet another for loop to loop our third hex set. This uses the same syntax as the other two, only we are using the variable $k.

for($k=0;$k<6;$k  ){

Now is where the fun part comes! We are now going to set a new variable called $hex that will contain our three hex sets combined. We will take the value of whatever placing from the array of $c that $i happens to be, then string it together with whatever the placing of the array $c that $j is, and the same for $k. This will make more sense when we explore how the entire script works later on.

$hex = $c[$i].$c[$j].$c[$k];

In the next line, we will simply echo a table cell with a background color of the $hex color, and have it output the hexidecimal value of that color. Simple as that.

echo '#'.$hex.'';

The next two lines simple close the third for loop (the one that loops $k) and echos the end of the table row.

}echo '';

The next four lines also simply close our remaining for loops, and then echos the end of the table.

}}echo '';}

The last line of course closes our PHP code.

Now, since this is a function, we can easily call this entire hex chart from anywhere in a script by simply calling the function name with parenthesis, inside of PHP tags of course.

Alright, so now we have our code and understand more or less what it does, but what exactly does it do? Let's analyze the simple but possibly confusing for loops.

The way this script works, is it will first go through the first for loop, logically. On the first passing, $i is equal to 0.

Now, it will move onto the second for loop. We are still on the very first passing, so $j is equal to 0 as well. Since we are in this loop, the first line to execute is to echo a table row, so we do just that.

We then move onto the third for loop, where $k is equal to 0.

Now, as I've said, we are on our first passing. All three variables are set to 0; $i, $j, and $k. So, our $hex variable will be set to $c[0] strung together 3 times. Since these numbers are in brackets, they represent a key. 0 is the very first place of an array. In our array $c, the first place is '00'. So basically, we can imagine it like this.

$hex = '00'.'00'.'00';

Make sense? Alright, now we will echo a simple table cell with this $hex background color (with a pound sign!), then echo that same value inside the table cell so we can see what hexidecimal value makes that color.

Alright, so we've made on passing through the third loop. But wait, we are supposed to loop through this one while $k is less than 6. Here we go on our second trip through the third for loop.

Since we are not ready to advance to the first or second for loops again, $i and $j are still 0, but, $k is now equal to 1. Now, our $hex will basically look like this.

$hex = '00'.'00'.'33';

And so on. We continue looping through the third loop until $k is equal to 5 basically, thus making 6 cells in a row.

Now, since we have completed this loop, we will advance once on the second loop (the $j loop). Now, remember, $i is still 0, but now $j equals 1! $k is reset to 0 since we are on a completely different loop.

So now basically we will start looping through $k the same as last time, only this time, $j is 1, so our $hex values will be slightly different. On the first passing, our string will look pretty much like this.

$hex = '00'.'33'.'00';

Alright, so we continue this pattern until $j is equal to 5, meaning we will end up with 6 total loops. Finally we can advance once with the first loop ($i loop)!

We then get to start the entire thing over, only this time, $i is equal to 1! So, basically on our very first passing, we will get our $hex value looking like this.

$hex = '33'.'00'.'00';

And we continue this entire process until $i is equal to 5, meaning the entire process has been completed 6 times. That makes 36 rows, 216 cells!

Better than typing those 216 individual cells yourself, eh?

Thanks for reading, and I hope you have learned something. =)

If you would like more help with this tutorial please register and visit the forum.

About Michael

Michael Dunlop Michael Dunlop personal statement

“But I am already older than 21”, I hear you cry. Don’t worry, despite the name, the information and advice is more geared towards retiring prior to having a fake hip installed.

Website Sponsors
Recommended Websites
They Buy, Build, Upgrade and Sell Websites.
 
Advertise Here
Feature Interviews
Graham went from $100 one day to ... Read more
Paul Bourque started affiliate marketing less than ... Read more
Interview with Yaro Starak of Entrepreneurs-Journey.com is ... Read more
Recommended Resources
 
 
 
 
 
Get Retire at 21's FREE Secrets
Find out how I'm going to Retire VERY Early Subscribe for my Free Secrets Privacy Guaranteed. 100% Spam free.