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:
For example while loop that counts to 10
see the code :
Syntax for the For Loops are
Sample script for for loops:
See The Code :
Stopping a Loop example to avoid the Loop:
See The Code :
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 :
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;Out Put :
while ($ num <= 10) {
print "Number is $ num
n ";
$ num + +;
}
print 'Done.';
?>
Number is 12. For Loops
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
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 + +) {Out Put :
print "Number is $ num
n ";
}
?>
Number is 13. Stopping a Loop
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
Stopping a Loop example to avoid the Loop:
See The Code :
$ counter = -3;Out Put :
for (; $ counter <10; $ counter + +) {
/ / Check for division by zero
if ($ counter == 0) {
echo "Stopping to Avoid zero.";
break;
}
echo "100 / $ counter
";
}
?>
100/-3Stopping to Avoid division by zero.
100/-2
100/-1
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;Out Put :
for (; $ counter <10; $ counter + +) {
/ / Check for division by zero
if ($ counter == 0) {
echo "Skipping to Avoid a zero.
";
continue;
}
echo "100 / $ counter
";
}
?>
100/-3He 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
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
0 comments on Looping In PHP :
Post a Comment and Don't Spam!
Dont Spam please