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

Revisiting The Past - 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


So that's the "while" loop - it executes a set of statements until a specified condition is satisfied. But what happens if the condition is satisfied on the first iteration itself? In the illustration above, for example, if you were to enter the year 2000, the "while" loop would not execute even once. Try it yourself and you'll see what we mean.

So, if you're in a situation where you need to execute a set of statements *at least* once, PHP offers you the "do-while" loop. Here's what it looks like:

do { do this! } while (condition) Let's take a quick example: <?php $bingo = 366; while ($bingo == 699) { echo "Bingo!"; break; } ?>
In this case, no matter how many times you run this PHP script, you will get no output at all, since the value of $bingo is not equal to 699. But, if you ran this version of the script

<?php $bingo = 799; do { echo "Bingo!"; break; } while ($bingo == 699); ?>
you'd get one line of output.

Thus, the construction of the "do-while" loop is such that the statements within the loop are executed first, and the condition to be tested is checked after. This implies that the statements within the curly braces would be executed at least once.

Let's modify the time machine above to use a "do-while" loop instead:

<? // if form has not been submitted, display initial page if (!$submit) { ?> <html> <head> </head> <body> <h2>The Incredible Amazing Fantabulous Time Machine</h2> <form action="tmachine.php4" method="POST"> Which year would you like to visit? <input type="text" name="year" size="4" maxlength="4"> <input type="submit" name="submit" value="Go"> </form> </body> </html> <? } else // else process it and generate a new page { ?> <html> <head> </head> <body> <? // current year $current = 2000; // check for dates in future and generate appropriate message if ($year > $current) { echo "<h2>Oops!</h2>"; echo "Sorry, this time machine can only travel backwards at the moment. But leave your phone number and we'll call you when the new improved model goes on sale."; } else { // or echo "<b>Going back to $year...</b><br>"; // in this case, the "do-while" loop // ensures that at least one line of output is generated // even when you enter the year 2000 do { $current = $current - 1; echo "$current "; } while($year < $current); echo "<br><b>The past definitely isn't all it's cracked up to be!</b>"; } ?> </body> </html> <? } ?>

>>> 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: