Online Book Reader

Home Category

HTML, XHTML and CSS All-In-One for Dummies - Andy Harris [217]

By Root 1477 0
a debugging tool. Typically, you’ll use some other techniques for displaying arrays to your users.

To see what all the code in basicArray.php looks like, take a look at Figure 4-1.

Figure 4-1: Arrays are pretty easy to use in PHP.

Preloading an array

Sometimes you’ll know the elements that go into an array right away. In those cases, you can use a special version of the array() function to make this work. Take a look at this code.

$english = array(“zero”, “one”, “two”, “three”);

print “

 \n”;

print_r($english);

print “

 \n”;

This simple program allows you to load up the value of the array in one swoop. Note that I started with zero. Computers tend to start counting at zero, so if you don’t specify indices, the first element will be zero-indexed.

I use the print_r() function to quickly see the contents of the array. The preloaded array is shown in Figure 4-2.

Figure 4-2: This array was preloaded, but the user can’t tell the difference.

Using Loops with Arrays

Arrays and loops are like peanut butter and jelly; they just go together. When you start to use arrays, eventually, you’ll want to go through each element in the array and do something with it. The for loop is the perfect way to do this.

Look at the loopingArrays.php code to see how I step through an array with a couple of variations of the for loop.

loopingArrays.php

Looping through arrays.

//first make an array of mini-book names

$books = array(”Creating the XHTML Foundation”,

”Styling with CSS”,

”Using Positional CSS for Layout”,

”Client-Side Programming with JavaScript”,

”Server-Side Programming with PHP”,

”Databases with MySQL”,

”Into the Future with AJAX”,

”Moving From Pages to Web Sites”);

//just print them out with a loop

print ”

\n”;

for ($i = 0; $i < sizeof($books);$i++){

print $books[$i] . ”
\n”;

} // end for

print “

\n”;

//use the foreach mechanism to simplify printing out the elements

print ”

\n”;

foreach ($books as $book){

print $book . ”
\n”;

} // end foreach

print ”

\n”;

?>

The relationship between arrays and loops isn’t hard to see:

1. Create your array.

This example uses an array of minibook titles in a charming and lovable book on Web development. Note that I preloaded the array. There’s no problem with the fact that the array statement (while a single line of logic) actually takes up several lines in the editor.

2. Build a for loop to step through the array.

The loop needs to happen once for each element in the array; in this case, that’s eight times. Set up a loop that repeats eight times. It will start at 0 and end at 8.

3. Use the sizeof() function to determine the ending point.

Because you know that this array has eight elements, you could just set the condition to $i < 8. The sizeof() function is preferred because it will work even if the array size changes. Also, it’s easier to understand what I meant. sizeof($books) means “the size of the $books array.” The number 8 could mean anything.

4. Print out each element.

Inside the loop, I simply print out the current element of the array, which will be $books[$i]. Don’t forget to add a
tag if you want a line break in the HTML output. Add the \n to keep the HTML source code looking nice.


Simplifying loops with foreach

The relationship between loops and arrays is so close that many languages provide a special version of the for loop just for arrays. Take a look at this code fragment to see how cool it is:

//use the foreach mechanism to simplify printing out the elements

print “

\n”;

foreach ($books as $book){

print $book . “
\n”;

} // end foreach

print “

\n”;

The

Return Main Page Previous Page Next Page

®Online Book Reader