Loops are the essential part of the program that have instructions to repeat the code itself again and again until certain conditions are met. More than one loops can be used several times in a script.
Loops makes easy to write the program, saves times and shorten the scripting. It instruct the codes to repeat again and again until the result found or the number of times instructed for repetition of the code.
In PHP, like other programming language, it has following looping statements:
3.10.1. While Loop
While loops are usually used for incrementing and decrementing a variable until the repetitions are met to a maximum or minimum value that you've set. The while loop says,
while (something is true)
// do something that you specify
Using While loop we can print the counting from 1 to n- numbers. Here N refers to limit from which the code will run.
a. the variable $Counting = 1;
b. print $ Counting;
c. add 1 to $ Counting;
d. go to sep a. and run this script again with the new value of $ Counting;
d. stop when $MyNumber reaches 11;
The syntax for incrementing and decrementing the value of a variable is:
adds 1 to the value of the variable $a each time through
subtracts 1 from the value of the variable $a each time through
So the code itself for printing the numbers 1 to 10 may look like this:
$Counting = 1;
while ($Counting <= 10)
Posted on: July 10, 2009 If you enjoyed this post then why not add us on Google+? Add us to your Circles