- While Loop
- Do-While Loop
- For Loop
- Foreach Loop
While Loop
Syntax
While (condition)
{ code to be executed. }
Example
<?php $count=0; while ($count!=10) { echo $count."\n"; $count++; } ?>The output of the above example will be counting from 0 to 9.
Do-While Loop
Syntax
Do
{ Code to be executed. }
While (condition)
Example
<?php $count=0; do { echo $count."\n"; $count++; } while ($count!=10) ?>The output of the above example will be counting from 0 to 9.
For Loop
Syntax
for (initialize; condition; increment)
{ code to be executed. }
Example
<?php for ($i=0; $i<=10; $i++) { echo $count."\n"; } ?>The output of the above example will be counting from 0 to 10.
Foreach Loop
Syntax
foreach ($array as $value)
{ code to be executed. }
Example
<?php $count=array(0,1,2,3,4,5,6,7,8,9); foreach ($count as $value) { echo $value . "\n"; } ?>The output of the above example will be counting from 0 to 9.
References
http://www.php.net
http://www.w3schools.com
Ok guyz this is all for loops in php, keep practicing and for any queries don’t hesitate to comment and ask.
Previous Lesson --- Next Lesson
No comments:
Post a Comment