SQL PHP HTML ASP JavaScript articles and free scripts to download If you are facing any problem in viewing this page, please tell us
 

PHP For & foreach looping

For loop in PHP is used to iterate through a section in an application. This is used frequently in many applications. Different requirements can be achieved like breaking the loop based on some condition by using break statement etc can be added. We will start with a basic loop and then move to adding different requirements to it. Here is the basic syntax for for loop.

for($i=1; $i<=10; $i++){
echo $i."<br>";
}


This will list from number one to ten. Now we will try with a break statement in side the for loop.


Related Tutorial
PHP switch
While loop
PHP file upload
PHP Signup script
for($i=1; $i<=10; $i++){
if($i > 5){break;}
echo $i."<br>";
}

This will list from 1 to 5 only. The If condition inside the for loop will exit the loop when the value of $i exceeds 5 ( that is when it equals to 6)

While handling PHP Arrays foreach function is required to display the elements of an array. Once the foreach function is called the array pointer will reset to the first element of the array. So we need not call the reset() again( Reset() rewinds array’s internal pointer to the first element. ). Here is an example of the foreach code.

$i = array (10, 12, 13, 25);
foreach ($i as $v) {
print "Present value of \$i: $v <br> ";
}

This will list all the values of the array from starting to end. You can read more on arrays
Further readings
Loops and program structure of PHP scripts
Checking variables by isset
switch
For Loop
if else condition checking
while loop
Do while loop
PHP beginners guide

 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorials
Basic Loops
PHP Monthly Planner
PHP Introduction
Loops & structure
Array
Date & Time
Functions
Form Handling
File Handling
Math Functions
String Functions
GD Functions
Comment Posting
Content Management
PHP & Ajax
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.