Student Of Fortune

Looping In PHP

Share on :
Now I will try to explain about PHP Looping on this occasion. Php loops we can use some functions, such as: while loops, For Loops, Breaking Out of a Loop, continue Statements.
  
1. While Loops
Syntax for the while loop:


while (expression)
{
code to
execute;
}

For example while loop that counts to 10
see the code : 

$ num = 1;

while ($ num <= 10) {
print "Number is $ num
n ";
$ num + +;
}

print 'Done.';
?>
Out Put :


Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
2. For Loops
Syntax for the For Loops are

for (initialization expression; test expression; modification expression) {
Code That Is Executed;
}

Sample script for for loops:
See The Code :

for ($ num = 1; $ num <= 10; $ num + +) {
print "Number is $ num
n ";
}
?>
Out Put :

Number is 1
Number is 2
Number is 3
Number is 4
Number is 5
Number is 6
Number is 7
Number is 8
Number is 9
Number is 10
3. Stopping a Loop
Stopping a Loop example to avoid the Loop:
See The Code :

$ counter = -3;

for (; $ counter <10; $ counter + +) {
/ / Check for division by zero
if ($ counter == 0) {
echo "Stopping to Avoid zero.";
break;
}

echo "100 / $ counter
";
}

?>
Out Put :


100/-3
100/-2
100/-1
Stopping to Avoid division by zero.
he will loop until the function if and because there are scripts break, then the process will stop

4. Continue Statements 
Continue the use in addition to break
See The Code :

$ counter =- 3;

for (; $ counter <10; $ counter + +) {
/ / Check for division by zero
if ($ counter == 0) {
echo "Skipping to Avoid a zero.
";
continue;
}

echo "100 / $ counter
";
}

?>
Out Put : 
100/-3
100/-2
100/-1
Skipping to Avoid zero.
100 / 1
100 / 2
100 / 3
100 / 4
100 / 5
100 / 6
100 / 7
100 / 8
100 / 9
He will loop until the function if and as there continue the script, then the process will stop changing the numbers from 0 into Avoid skipping to zero, then looping will be forwarded back

0 comments on Looping In PHP :

Post a Comment and Don't Spam!

Dont Spam please

 
Recommended Post Slide Out For Blogger

Recent Comments

My Rank