Saturday, 19 October 2013

While Loops in php

While Loops in php
This tutorial is of While Loops. In php,why we need loops during programming ? Here we may repeat certain amount of actions for a specified amount of times.
Code for While Loop (Screenshot1):-
While Loop in  php
While Loop(Screenshot1)
1
2
3
4
5
6
7
<?php
 
while(1) {
echo 'Hello <br> ';
}
 
?>
Output (Screenshot2) :-
While Loop(Screenshot2)
While Loop(Screenshot2)
In this example we have echoed out “Hello” by while loop.In this loop 1 evaluates true which echo out “Hello” but what if we want to echo out “Hello” 100 times! Let see example,how to echo out “Hello” for 10 times.
Code for While Loop (Screenshot3):-
While Loop in php
While Loop(Screenshot3)
1
2
3
4
5
6
7
8
9
<?php
 
$counter = 1;
 
While ($counter<=10) {
echo 'Hello<br>';
$counter++;
}
?>
Output (Screenshot4):-
While Loop in php
While Loop(Screenshot4)
In this example, we have starter counter which initialize/starts loop and condition counter which must be less than or equal to 10 that will be incremented again and again upto the limit of condition counter and thus echo out “Hello” 10 times.
Code for While loop (Screenshot5):-
While Loop in php
While Loop(Screenshot5)
1
2
3
4
5
6
7
8
9
<?php
 
$counter = 11;
 
While ($counter<=10) {
echo 'Hello<br>';
$counter++;
}
?>
Ouput (Screenshot6) :-
While Loop in php
While Loop(Screenshot6)
In this example condition will false and will not display “Hello” because condition counter is greater than 10.
Read More Extra Tutorials

No comments:

Post a Comment