The for loop is designed to perform the specific statements for a determined number of iterations (unlike while, which runs until the condition is FALSEsimilar, but significantly different, concepts). You normally use a dummy variable in the loop for this purpose. The for loop's syntax is more complicated than the while loop and although the uses of these loops can easily overlap, you'll find one more suited to some tasks than the other. for (initial expression; condition; closing expression) { statement(s); } The initial expression will be executed once, the very first time the loop is called. Then the condition is used to determine whether or not to execute the statements. Finally, the closing expression will be executed after each time that the condition is found to be TRUE, but only after the statements are executed. Thus, to print out each value in an array, you would code: for ($n = 0; $n < count($Array); $n++) { print ("$Array[$n]<BR>\n"); } It may help your comprehension of the for loop syntax if I were to rewrite the $Day while loop from Script 6.9 as a for loop. The original code was: $Day = 1; while ($Day <= 31) { print ("<OPTION VALUE=$Day>$Day </OPTION>\n"); $Day++; } First the value of $Day was set, then the while loop stated the conditional ( $Day <= 31 ). Finally, if TRUE, the print() statement was executed and $Day was incremented. As a for loop, that same code would look like this: for ($Day = 1; $Day <= 31; $Day++) { print ("<OPTION VALUE=$Day>$Day</OPTION>\n"); } Script 6.10. This short script is a simple use of the for loop, reiterating through a process 1000 times. You'll use the for loop to print out all the prime numbers between 1 and 1000, a common programming example. To write a for loop: -
Create a new PHP document in your text editor. Type (Script 6.10): <HTML><HEAD><TITLE>Prime Numbers </TITLE><BODY><?php -
Begin the for loop. for ($n = 1; $n <= 1000; $n++) { -
Using the syntax of the for loop, $n is established as a dummy variable. It is immediately set to 1, then the loop will check to see if $n is less than or equal to 1000. In other words, this loop will be executed 1000 times. Finally, if the condition of the loop ( $n <= 1000 ) is TRUE, the statement(s) will be executed, $n will be incremented, and the process will start again. -
Write the contents (i.e., the statement(s) section) for the loop. if ( ($n == 1) OR ($n == 2) OR ($n == 3) OR ($n == 5)) { print("$n<BR>\n"); } elseif (($n % 2 != 0) AND ($n % 3 != 0) AND ($n % 5 != 0)) { print("$n<BR>\n"); } A number is prime, if it is not cleanly divisible by any numbers other than itself and 1. In other words, if you divide the number by any other number than the number itself and 1, and you always get a remainder, then the number is prime. For example, 4 can be divided cleanly by 2 so it is not prime whereas 7 cannot be cleanly divided by 2, 3, or 5, which are the basis for establishing prime. Offhand, you know that 1, 2, 3, and 5 are primes, so if $n is equal to any of these, it will be printed automatically. Notice that I used the logical OR operator because if any of those conditions are TRUE, you'll want to print out $n. If $n is not equal to 1, 2, 3, or 5, you'll have to check it for prime. To do so, you see if the division of $n by 2, 3, or 5 leaves a remainder, calculated using the modulus operator (%). Modulus simply returns the remainder of a division and while it's not the most useful operator, it's invaluable here. So 4 modulus 2 is 0 and 7 modulus 5 is .4 (four-tenths). If a non-zero remainder is returned when dividing $n by 2, 3, and 5, the number is a prime and should be printed. Notice that I used the logical AND operator because if any of those conditions are FALSE, the number is not prime. -
Close the for loop, the PHP, and the HTML. } ?> </BODY> </HTML> -
Save the script as primes.php, upload it to your server, and test in your Web browser (Figure 6.17). Figure 6.17. This is only the listing of the primes through 100, automatically calculated and spit out using the for loop. Here the 16 lines of Script 6.10 created a total of approximately 300 lines in the HTML window. Tip Although there is a fair amount of overlap as to when the two major loop constructs, while and for, can be used, you will discover as you program that sometimes one is more logical than the other. The while loop is frequently used in the retrieval of data from a database (see Chapter 11, Databases) and the for loop is a standard for working with arrays (as you'll discover in Chapter 7, Using Arrays). |