Showing posts with label php loops. Show all posts
Showing posts with label php loops. Show all posts

11 January 2011

Php For Beginners (Lesson 7)

In this lesson we will discuss loops in php. Loops are used to run a piece of code for a certain number of times. Php supports four types of loops:

  1. While Loop
  2. Do-While Loop
  3. For Loop
  4. 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.


next Previous Lesson --- Next Lesson next