• May 20, 2012, 07:42:03 PM
  • Welcome, Guest
Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Lesson 4: Loops and Arrays  (Read 78 times)

williebonney

  • Claymore Magnet
  • Tactical General
  • Mack Daddy
  • *
  • Posts: 1,196
  • I'm all out of bubblegum
    • View Profile
    • Total Eclipse
Lesson 4: Loops and Arrays
« on: April 30, 2011, 10:03:55 PM »

Loops and arrays are some of the most important concepts to grasp in php and really in any programming language.  They are important just as themselves, and also because they lay the groundwork for more sophisticated code, such as making objects.

Up until now, we've really just dealt with one piece of data.  An array is a collection of data, and loops let you go through each piece of that collection one at a time and do something with them.

For example, think about the General Talk category page in our forums.  When you go to that page, you see a list of many different threads.  Each thread is an individual item, and of those individual items are collected together and displayed in one place on that page.

So in that example, an array holds the names of each of those threads, and then a loop goes through that array and displays each thread name on the page.

So here I'll create an array called $carMakers, which will hold the names of several car manufacturers.

Code: [Select]
<?php

$carMakers 
= array("Ford""Chevy""Nissan""Toyota");

?>



An array puts everything in a line.  So Ford is first in line, Chevy is second in line, etc.  But in programming languages, the first item in an array is in position 0, the second item is in position 1, etc.  (don't ask me why).  So if you want to display Nissan on the page, you'd do it like this

Code: [Select]
<?php
echo $carMakers[2];
?>


The 2 in the brackets indicates that I'm calling position 2, which remember is the third item in the array. 

Now I only have 4 items in this array, so to print out each car manufacturer on the page using the above manner is relatively easy.  But what if the size of the array is huge and/or constantly changing?  Think back to the example of the forum threads.  There's lots of threads and the number of them is always going up.  Using the method above, you'd need 1000 of those echo lines to print out 1000 threads.  That would be a ton of code.  And then if there were 1001 threads, the 1001 thread wouldn't get displayed. 

That's where loops come in.  They let you print out each item in the array, no matter how many items there are, and with very little code.

Below I've created a loop to loop through the carMakers array and display each item.

Code: [Select]
<?php

$carMakers 
= array("Ford""Chevy""Nissan""Toyota");

for (
$i=0$icount($carMakers);  $i++) {
 
echo $carMakers[$i];
 
echo "<br>";
}

?>



Let's address the items in the parenthesis of the for loop first. 

$i=0 creates a variable called $i, and sets its value to 0.

count($carMakers) gives us the number of items in the $carMakers array.  So  it gives us 4.

So $i< count($carMakers) says that while the value of $i is less than the number of items in the $carMakers array, we're going to do whatever is in this loop.

$i++ says that each time we do whatever is in the loop, add one to $i. 

So remember, the value of $i starts out as 0.  So when we go into the loop the first line is this:
echo $carMakers[$i];

which means this:
echo $carMakers[0];

And since "Ford" is the first item in the array, that prints out "Ford."

The next line, echo "<br>"; simply adds a line break after "Ford"

So after those are printed out, 1 is added to $i, so the value of $i is 1.  And since 1 is less than 4, we go through the loop again.  So this time through, when we echo $carMakers[$i], we're really doing echo $carMakers[1], which will print out "Chevy."

Then the loop just continues until it prints out every item in the array.  So ultimately, what it prints out on the page is this:

Ford
Chevy
Nissan
Toyota
Logged
King of using little guns that shouldn't do what they do and finding stray claymores