Home Tutorials IT Jobs Source Codes Certifications Discussion Forum
  PHP Tutorials
PHP Introduction
PHP Installation
Syntax
Variables
Echo
Strings
Operators
Comments
Require
Include
If...Else
Switch
Forms
Functions
Arrays
While Loop
For Loop
For Each
Do While
Post & Get
File Create
File Open
File Close
File Write / Read
File Delete
File Append
File Truncate
File upload
PHP str replace
PHP substr_replace
PHP Capitalization
PHP Explode
PHP Implode
   IT Jobs
Software Jobs
Networking Jobs
   Model Question Papers
BE Computer Science
MCA
BCA
Others
 
   

PHP - for loop

A FOR loop is very similar to a WHILE loop in that it continues to process a block of code until a statement becomes false, however everything is defined in a single line. The basic structure for a FOR loop is:

for ( start; conditional; increment) { code to execute; }

Let's go back to our first example using the WHILE loop, where we printed out the numbers 1 through 10 and do the same thing using a FOR loop.

<?php 
for ($num=1; $num <= 10; $num++ ) 
{ 
print $num . " "; 
} 
?>

The FOR loop can also be used in conjunction with a conditional, just like we did with the WHILE loop:

<?php 
for ($num=1; $num <= 10; $num++ ) 
{ 
if ($num < 5) 
{ 
print $num . " is less than 5
<br>"; 
} 
else 
{ 
print $num . " is not less than 5 <br>"; 
} 
} 
?>

 

<< Previous | Next >>
Home  |  About us  | Privacy  |  Disclaimer  |  Contact us |  Advertise with us | Our Link Partners
All Rights Reserved 2009, CodeTeller.com