School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,455 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,594 people online right now. Registration is fast and FREE... Join Now!




Looping statements in PHP

 
Reply to this topicStart new topic

> Looping statements in PHP

shadhin
Group Icon



post 28 Jan, 2009 - 10:14 AM
Post #1


Looping statements in PHP

Looping statements are used to perform an action to a number of times. There are four looping statements in PHP-

1.while statement
2. do...while statement
3. for loop
4.foreach loop

while statement

The while statement is used to repeatedly execute a block of statements as long as a specified condition is true. The syntax is as follows-

CODE

while (condition)
{
code to be executed;
}

Example:while statement
[code]
<?php
$i=0;
while($i<5)
{
print "Hello World!"."<br/>";
$i++;
}
?>


Output:
Hello World!
Hello World!
Hello World!
Hello World!
Hello World!

do...while statement

Like while statement, do...while statement is another looping statements used in PHP. The basic difference with while statement is that, in case of while statement, the condition is checked at the begining and if the condiotion is false, the statement block is never executed. On the other hand, in case of do...while statement, the condition is checked at the end of the loop and the statement block is executed for at least once even the condition is initially false. The structure of do...while loop is as follows-

CODE

do
{
code to be executed;
}
while(condition);


Example:do...while statement
CODE

<?php
$i=0;
do
{
$i++;
print $i.”<br/>”;
}
while ($i<5);
?>


Output:
1
2
3
4
5

for loop

Generally while loops are used when the number of passes of the loop is not known. But, when the number of passes is known, it is better to use another looping statement which is for loop. Here is the general syntax of for loop-

CODE

for (initialization; condition; increment)
{
code to be executed;
}


Example:for loop
CODE

<?php
for ($i=0; $i<5; $i++)
{
print $i.”<br/>”;
}
?>


Output:
0
1
2
3
4

foreach loop

PHP provides another looping statement which is used to loop through arrays. The structure of foreach loop is as follows-

CODE

foreach (array as value)
{
code to be executed;
}


Example:foreach loop
CODE

<?php
$a=array("atik"=>20,"pervej"=>21,"jahid"=>18);
foreach($a as $name=>$age)
{
print "Name:".$name." Age:".$age."<br/>";
}
?>


Output:
Name:atik Age:20
Name:pervej Age:21
Name:jahid Age:18
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/8/09 01:59AM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month