Home arrow PHP arrow Page 4 - PHP 101 (part 3) - Chocolate Fudge And Time Machines

Doing It By The Numbers - PHP

After teaching you the fundamentals of form processing, PHP 101 returns with an explanation of WHILE, FOR and FOREACH loops, those PHP constructs that can save you hours of unnecessary HTML coding. Also included: array variables, the auto-increment operator, and some yummy desserts.

  1. PHP 101 (part 3) - Chocolate Fudge And Time Machines
  2. Back To The Future
  3. Revisiting The Past
  4. Doing It By The Numbers
  5. Anyone For Apple Pie?
  6. The Generation Gap
  7. What's That Noise?
  8. Checking The Boxes
  9. Miscellaneous Notes
By: Vikram Vaswani and Harish Kamath, (c) Melonfire
Rating: starstarstarstarstar / 6
August 15, 2000

print this article


Both the "while" and "do-while" loops continue to iterate for so long as the specified conditional expression remains true. But there often arises a need to execute a certain set of statements a specific number of times - for example, printing a series of thirteen sequential numbers, or repeating a particular set of <TD>> cells five times. In such cases, clever programmers reach for the "for" loop...

The "for" loop typically looks like this:

for (initial value of counter; condition; update counter) { do this! }

Looks like gibberish? Well, hang in there for a minute...the "counter" here is a PHP variable that is initialized to a numeric value, and keeps track of the number of times the loop is executed. Before each execution of the loop, the "condition" is tested - if it evaluates to true, the loop will execute once more and the counter will be appropriately incremented; if it evaluates to false, the loop will be broken and the lines following it will be executed instead.

And here's a simple example that demonstrates how this loop can be used:

<html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, PHP-Style!</center> <br> <? // define the number $number = 7; // use a for loop to calculate tables for that number for ($x=1; $x<=15; $x++) { echo "$number X $x = " . ($number*$x) . "<br>"; } ?> </body> </html>
Let's dissect this a little bit:

The first thing we've done here is define the number to be used for the multiplication table; we've used 7, since we're particularly poor at math - you might prefer to use another number.

Next we've constructed a "for" loop with $x as the counter variable - we've initialized it to 1 and specified that the loop should run no more than 15 times.

The $x++ you see in the "for" statement is an interesting little operator known as the auto-increment operator - more on this in the "Miscellaneous Notes" section below. For the moment, all you need to know is that it automatically increments the counter by 1 every time the loop is executed.

Finally, the line that does all the work - the "echo" statement, which takes the number specified, multiplies it by the current value of the counter, and displays the result on the page.

As you can see, a "for" loop is a very interesting - and useful - programming construct. Our next example illustrates its usefulness in a manner that should endear it to any HTML programmer.

<html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, Part II</center> <br> <table border=1> <? // the purpose of this exercise is // to generate a 4x4 grid via an html table // this is accomplished via two nested "for" loops // the first one is for the table rows or <tr>s // we need four of them for ($alpha=1; $alpha<=4; $alpha++) { ?> <tr> <? // the second one is for the cells in each row or <td>s // we need four of them too for ($beta=1; $beta<=4; $beta++) { ?> <td> <? // print the coordinate of each cell echo("row $alpha, column $beta"); ?> </td> <? } ?> </tr> <? } ?> </table> </body> </html>
And here's the output:

<html> <head> <basefont face="Arial"> </head> <body> <center>Turning The Tables, Part II</center> <br> <table border=1> <tr> <td> row 1, column 1 </td> <td> row 1, column 2 </td> <td> row 1, column 3 </td> <td> row 1, column 4 </td> </tr> <tr> <td> row 2, column 1 </td> <td> row 2, column 2 </td> <td> row 2, column 3 </td> <td> row 2, column 4 </td> </tr> <tr> <td> row 3, column 1 </td> <td> row 3, column 2 </td> <td> row 3, column 3 </td> <td> row 3, column 4 </td> </tr> <tr> <td> row 4, column 1 </td> <td>s row 4, column 2 </td> <td> row 4, column 3 </td> <td> row 4, column 4 </td> </tr> </table> </body> </html>
As you'll see if you try coding the same thing by hand, PHP's "for" loop just saved you a whole lot of work!

>>> More PHP Articles          >>> More By Vikram Vaswani and Harish Kamath, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: