PHP Tutorial Part 6 [while, dowhile, for, foreach loops and array pointers ]
PHP Video Tutorial Part 6
Part 6: Documentation + Video [while,dowhile,for,foreach,loops and array]
Download Link :
Size: 4.92 MB
And Also you can check all older and Incoming tutorials here :
Tutorial Links
“Documentation“
Hello Friends,
Welcome To PHP Tutorial Part 6
Topic Covered: while, dowhile, for, foreach loops and array pointers
Well I think you read my first tutorial PHP all part, if not then kindly download from here:
- Go to PHP folder and download rest all Parts of PHP tutorial.
___________________________________________________________________
Today covered in this tutorial while, dowhile, for, foreach loops and array pointers
Loops
If some action happens over and over again then we need Loops. Loops allow us to execute a bit of code until the condition is satisfied. There are 4 main kinds of loops:
- while loops
- do-while loops
- for loops
- foreach loops
1. while Loop:
The while statement will execute a code as long as a condition is true. Take look at syntax:

<?php
$count=1;
while($count<=10)
{
echo “Count is ” . $count . “<br />”;
$count++; // ++ is used to increment
}
/*
The loop will run continue until the condition is true , i.e. the value of count is less than equal to 10. The loop will increment after every execution mean after each time loop runs.
*/
?>
2. dowhile Loop
The dowhile loop will execute a code at least once – then it will repeat the loop as long as a condition is true. Check the syntax above.
First loop will execute and then condition is checked.
The main difference between while and do while loop is in while first condition is check then program executed, but in case of do while first program will execute then it will check the condition. Means do while loop will always run at least once.
<?php
$count=0;
do
{
$count++;
echo “Count is ” . $count . “<br />”;
}
while ($count<=10);
/*
The loop will run continue untill the condition is true , i.e. the value of count is less than equal to 10. The loop will increment after every execution mean after each time loop runs. It will run atleast once always.The condition is checked after the execution of loop.
It shows output till 11 because condition is checked after the execution of loop.
*/
?>
3. for Loop
for loop is used when you know how many times you want to execute a loop.
The difference between while and for loop is that, while loop is used where we don’t know how many time loop execute and for loop is used where we know how many times we want to execute a loop.
Let’s take a look at syntax:

<?php
for ($count=0; $count<=10; $count++)
{
echo $count . “<br />”;
}
?>
4. foreach Loop
The foreach statement is only work with arrays, for looping in the array. Take a look at syntax above.
For every loop, the value of the $array is assigned to $value- so on the next loop and continue.
<?php
$numbers =array(5,10,15,20,25,30); // declaration of array
foreach($numbers as $num) { //numbers is the temp variable
echo $num . ” <br /> “;
}
?>
Next is Array Pointers:
Pointers point to the current value of an array by default it’s always the first value. When we start looping through array the computer moves the pointer along the array to get each value.
More clear with program:
<?php
$count =array(5,10,15,20,25);
// current function will gives the current value of array on which it points.
echo “First: ” . current($count) . “<br />” ;
// next function will move the pointer to next value.
next($count);
echo “Second” . current($count) . “<br />” ;
// reset function reset its default location i.e. first vaule.
reset($count);
echo “Third: ” . current($count) . “<br />” ;
?>
Hope you enjoying the tutorial. If any problems mail me at: encarta.mann@gmail.com
Thanks for reading this documentation. Continue in Next Edition ….
Thanks,
LM Team!






Bodyc
23 Jun, 2009
Where are you from? Is it a secret?
Root_Admin
23 Jun, 2009
hi dear,
sorry what you wanna say ?
Dirnov
24 Jun, 2009
Hello,
Where are you from? Is it a secret?
Have a nice day
Dirnov
WhiteLine
19 Apr, 2010
I’m looking forward to getting more information about this topic, don’t worry about negative opinions.