Basic Control Structures in PHP
By Darren W. Hedlund2005-06-20
Basic looping
If the ability to control what code in a script is run and under what conditions is the first fundamental part of any true programming language, then the ability to execute the same code multiple times is a very, very close second. Let's say you would like to write a script that outputs the numbers 1 through 5 on the web browser. How would you do this? One example would be:
<?php echo "1<br />"; echo "2<br />"; echo "3<br />"; echo "4<br />"; echo "5<br />"; ?>
Although that doesn't seem too overly complicated, consider outputting the numbers 1 through 100, or 1000, or even 1,000,000? Obviously, writing a script that contains a million echo statements is, at best, impractical. Beyond that, there are many more complex examples where the same piece of code is executed numerous times that would be impossible to duplicate in such an inefficient fashion as above. It is with this in mind that we introduce the while statement.
Tutorial Pages:
» Conditional blocks
» The if statement
» Basic looping
» The while statement
» Infinite loops
» Notes on embedding code blocks
