<div style='background-color: none transparent;'></div>

Pages

Home » » PHP Looping - For Loops

PHP Looping - For Loops

Loops execute a block of code a specified number of times, or  while a specified condition is true.
<h2>
The for Loop</h2>
The for loop is used when you know in advance how many times the script  should run.
<h3>
Syntax</h3>
<hr />

<span style="color: red; font-size: 130%;"><span style="font-weight: bold;">for (</span><i style="font-weight: bold;">init; condition; increment</i><span style="font-weight: bold;">)</span> <span style="font-weight: bold;">   {</span> <span style="font-weight: bold;">   </span><i style="font-weight: bold;">code to be executed;</i> <span style="font-weight: bold;">   } </span></span>
<hr />

Parameters:
<ul>
<li><i>init</i>: Mostly used to set a counter (but can be any code to be executed once at the beginning of the loop)</li>
<li><i>condition</i>: Evaluated for each loop iteration. If it evaluates to TRUE, the loop continues. If it evaluates to FALSE, the loop ends.</li>
<li><i>increment</i>: Mostly used to increment a counter (but can be any code to be executed at the end of   the loop)</li>
</ul>
<b>Note: </b>Each of the parameters above can be empty, or have multiple expressions  (separated by commas).
<h3>
Example</h3>
The example below defines a loop that starts with i=1. The loop will continue  to run as long as i is less than, or equal to 5. i will increase by 1 each time  the loop runs:
<hr />
<a href="http://www.blogger.com/post-edit.g?blogID=3313246643998194674&amp;postID=4775109265554140609"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5596027269847625058" src="http://1.bp.blogspot.com/-N5HEloA6_5g/TakVJySqrWI/AAAAAAAAAS0/oDgP4nt4gIE/s320/6.JPG" style="cursor: hand; cursor: pointer; height: 303px; width: 320px;" /></a>
<hr />
Output:
<hr />
<div style="color: red; font-weight: bold;">
<span style="font-size: 130%;">The number is 1
The number is 2
The number is 3
The number is 4
The number is 5</span></div>
<hr />
<h2>
The foreach Loop</h2>
The foreach loop is used to loop through arrays.
<h3>
Syntax</h3>
<hr />

<span style="color: red; font-size: 130%;"><span style="font-weight: bold;">foreach ($</span><i style="font-weight: bold;">array </i><span style="font-weight: bold;">as</span><i style="font-weight: bold;"> </i><span style="font-weight: bold;">$</span><i style="font-weight: bold;">value</i><span style="font-weight: bold;">)</span> <span style="font-weight: bold;">   {</span> <span style="font-weight: bold;">   </span><i style="font-weight: bold;">code to be executed;</i> <span style="font-weight: bold;">   } </span></span>
<hr />

For every loop iteration, the value of the  current array element is assigned to $value (and the array pointer is moved by one) -  so on the next loop iteration, you'll be looking at the next array value.
<h3>
Example</h3>
The following example demonstrates a loop that will print the values of   the given array:
<hr />
<a href="http://www.blogger.com/post-edit.g?blogID=3313246643998194674&amp;postID=4775109265554140609"><img alt="" border="0" id="BLOGGER_PHOTO_ID_5596028064205062898" src="http://3.bp.blogspot.com/-6tTR6YSvXFQ/TakV4BgPQvI/AAAAAAAAAS8/D9n2kNSYXb8/s320/7.JPG" style="cursor: hand; cursor: pointer; height: 303px; width: 320px;" /></a>
<hr />
Output:
<hr />

<span style="color: red; font-size: 130%;"><span style="font-weight: bold;">one</span>
<span style="font-weight: bold;"> two</span>
<span style="font-weight: bold;"> three</span></span>
<hr />
Share this article :

No comments :

Post a Comment

 
Copyright © 2011. datasavvy . All Rights Reserved
Company Info | Contact Us | Privacy policy | Term of use | Widget | Advertise with Us | Site map
Template Modify by Creating Website . Inpire by Darkmatter Rockettheme Proudly powered by Blogger