Thursday 9, February 2012
Welcome Guest, Register | Login  
      Home    |    Tutorials    |    Free Ebooks    |    Free Scripts    |    Articles    |    Blog     |    Forum    |    About Us    |    Contact Us

Loops

Loops are used to repeat statement or block of statement more than one time. The idea of a loop is to do something over and over again until the task has been completed.

In PHP we have the following looping statements:

 

For Loop:

For loops are useful constructions to loop through a finite set of data, such as data in an array.

Syntax:
for ( initialization; conditional statement; increment/decrement){
            statement(s);
}

  1. Initialization—You can pass in an already assigned variable or assign a value to the variable in the for statement.
  2. The condition required to continue the for loop— The statements are executed until the given condition goes false.
  3. Statement to modify the counter on each pass of the loop.
Example:
for ($i=1; $i<=10; $i++)
{
  echo "Hello World
";
}

?>

Foreach Loop:

Foreach loops allow you to quickly traverse through an array.

 

$array = array("name" => "Jet", "occupation" => "Bounty Hunter" );

foreach ($array as $val) {

    echo "

$val";

}

Prints out:

Jet

Bounty Hunter

 

You can also use foreach loops to get the key of the values in the array:

 

foreach ($array as $key => $val) {

    echo "

$key : $val";

}

 

Prints out:

name : Jet

occupation : Bounty Hunter

While Loop:

While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set.

 

Syntax:
while (condition){
statement(s);

}

 

Example:
$i=1;
while($i<=9)
  {
  echo "The number is " . $i . "
";
  $i++;
  }

?>

 

Do-While Loop:%
 
     
   
PHP Tutorial
 
  Introduction
Introduction
  Variables
Variables
  Strings
Strings
  Operators
Operators
  If Else Statement
If Else Statement
  Loops
Loops
  Functions
Functions
  Forms
Forms
  Sessions
Sessions
  Cookies
Cookies
  Files
Files
  E-mail
E-mail
  Error Handling
Error Handling
  File Upload
File Upload
  PHP Databases
PHP Databases
  Connecting to a MySQL Database
Connecting to a MySQL Database
  MySQL Data Types
MySQL Data Types
  Inserting Data Into a Table
Inserting Data Into a Table
  Select Statement
Select Statement
  Where Clause
Where Clause
  Order By
Order By
  Update Statement
Update Statement
  Delete From Statement
Delete From Statement
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download 30 Days To Create A Product Ebook 30 Days To Create A Product
   
Download 12 Simple Ways to Promote your Website Ebook 12 Simple Ways to Promote your Website
   
Download Make Money Online Ebook Make Money Online
   
Download An Introduction to Computer Security: The NIST Handbook  Ebook An Introduction to Computer Security: The NIST Handbook
   
Download Cracking The Niche Code Ebook Cracking The Niche Code
   
 
Studiesinn.com © 2012 All Rights Reserved.
Website Designed & Developed by TechXprtz