PHP Tutorial Part 6 [while, dowhile, for, foreach loops and array pointers ]

1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 5.00 out of 5)
Loading ... Loading ...

PHP Video Tutorial Part 6

Part 6: Documentation + Video  [while,dowhile,for,foreach,loops and array]

YouTube Preview Image

Download Link :

Part 6  download

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:

Download Older One’s

  • 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:

Syntax of while and do while loop

<?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:

syntax of for and foreach loop

<?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!

If you like it, then why not share with others and bookmark it :

  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogosphere News
  • email
  • Internetmedia
  • LinkedIn
  • Live
  • MSN Reporter
  • MySpace
  • PDF
  • SphereIt
  • StumbleUpon
  • Technorati
  • Twitter
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • RSS
  • blogmarks
  • Where are you from? Is it a secret? :)

  • hi dear,

    sorry what you wanna say ?

  • Hello,
    Where are you from? Is it a secret? :)
    Have a nice day
    Dirnov

  • I’m looking forward to getting more information about this topic, don’t worry about negative opinions.

You can follow any responses to this entry through the RSS 2.0 feed.