Get latest news and Web Designing tutoria

Saturday, 30 May 2015

PHP While Loops

Instead of using a for loop, you have the option to use a while loop. The structure of a while loop is more simple than a for loop, because you’re only evaluating the one condition. The loop goes round and round while the condition is true. When the condition is false, the programme breaks out of the while loop. Here’s the syntax for a while loop:
while (condition) {
statement

}
And here’s some code to try. All it does is increment a variable called counter:
$counter = 1;
while ($counter < 11) {
print (" counter = " . $counter . "<BR>");
$counter++;
}
The condition to test for is $counter < 11. Each time round the while loop, that condition is checked. If counter is less than eleven then the condition is true. When $counter is greater than eleven then the condition is false. A while loop will stop going round and round when a condition is false.
If you use a while loop, be careful that you don’t create an infinite loop. You’d create one of these if you didn’t provide a way for you condition to be evaluated as true. We can create an infinite loop with the while loop above. All we have to do is comment out the line where the $counter variable is incremented. Like this:
$counter = 1;
while ($counter < 11) {
print (" counter = " . $counter . "<BR>");
//$counter++;
}
Notice the two forward slashes before $counter++. This line will now be ignored. Because the loop is going round and round while counter is less than 11, the loop will never end – $counter will always be 1.
Here’s a while loop that prints out the 2 times table. Try it out in a script.
$start = 1;
$times = 2;
$answer = 0;
while ($start < 11) {
$answer = $start * $times;
print ($start . " times " . $times . " = " . $answer . "<BR>");
$start++;
}
The while loop calculates the 2 times tables, up to a ten times 2. Can you see what’s going on? Make sure you understand the code. If not, it’s a good idea to go back and read this section again. You won’t be considered a failure. Honest!
In the next part, we'll have a brief look at Do ... While loops

Related Posts:

  • Adding up in PHP OK, let's do some adding up. To add up in PHP, the plus symbol (+) is used. (If you still have the code open from the previous page, try changing the full stop to a plus symbol. Run the code, and see what happens.)… Read More
  • More variable practice In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll g… Read More
  • Substraction in php We're not going to weigh things down by subjecting you to torrents of heavy Math! But you do need to know how to use the basic operators. First up is subtracting. To add up using PHP variables, you did this: <… Read More
  • Joining direct text and Variable You can join together direct text, and whatever is in your variable. The full stop (period or dot, to some) is used for this. Suppose you want to print out the following "My variable contains the value of 10". In PH… Read More
  • More practice on Variables In the previous section, you saw what variables are: storage areas to hold things like numbers and text. You tell PHP to remember these values because you want to do something with them. In this section, you'll get… Read More

0 comments:

Post a Comment