 |
|
| Coding Forum Problems with your code? Discuss coding issues, including JavaScript, PHP & MySQL, HTML & CSS, Flash & ActionScript, and more. |
|
 |

09-05-2011, 01:03 AM
|
|
No Longer Active
Latest Blog: None
|
|
Join Date: 12-30-10
Location: Dhaka
Posts: 1,350
|
|
|
What is the difference between loops in php?
I have seen for, foreach and while loop in php. They seem to be doing the same thing. So why do we have all these different types of Loops in php?
|

09-05-2011, 01:28 AM
|
|
The Controversial Coder
Latest Blog: None
|
|
Join Date: 05-01-06
Location: Manchester; UK
Posts: 2,376
|
|
Undoubtedly this will explain the differentiation far more eloquently than I could.
|

09-05-2011, 03:10 AM
|
 |
Contributing Member
|
|
Join Date: 07-05-11
Location: philippines
Posts: 312
|
|
Quote:
Originally Posted by ameerulislam
I have seen for, foreach and while loop in php. They seem to be doing the same thing. So why do we have all these different types of Loops in php?
|
they differ in speed and performance..
|

09-05-2011, 06:11 AM
|
|
Contributing Member
Latest Blog: None
|
|
Join Date: 08-15-11
Posts: 50
|
|
|
You can get most loop mechanisms to do the same thing generally speaking, but what's important really is how your code reads and preventing stupid mistakes as much as possible when you can.
Sometimes it's more eloquent to loop through an object or iterator using a foreach because of how it pulls out the current key/value pair, or because you have an object that implements the ArrayIterator interface.
Sometimes while loops are better due to having an object that handles looping internally and returns true or false with a method (such as a database result set having a fetch() or retrieve() method, e.g. while ($result->fetch()) { ... }) and so on.
For loops are great for when you need to perform an operation a set amount of times and you can provide a counter initialization and increment/decrement appropriately all in one place, which makes it clean and helps you to make sure you don't miss important declarations like incrementing the counter, which could cause an infinite loop.
The different looping structures help to make your code more human readable, which, at the end of the day, makes the code more self-documenting and makes it easier for you to write clean and quality code.
|

09-05-2011, 06:27 AM
|
|
No Longer Active
Latest Blog: None
|
|
Join Date: 12-30-10
Location: Dhaka
Posts: 1,350
|
|
Quote:
Originally Posted by JohnnyS
|
Is the link you gave relevant? coz it's the root domain. Your link is not opening here anyways.
|

09-12-2011, 02:22 AM
|
 |
Contributing Member
|
|
Join Date: 07-05-11
Location: philippines
Posts: 312
|
|
Quote:
Originally Posted by ameerulislam
Is the link you gave relevant? coz it's the root domain. Your link is not opening here anyways.
|
here's the link: http://www.phpbench.com/
|

09-12-2011, 02:44 AM
|
|
Banned
Latest Blog: None
|
|
Join Date: 07-13-11
Location: In Fr0nt 0f C0mputer
Posts: 132
|
|
|
Agreed with Defrag & PHP is a server-side scripting language, like ASP & Loop is a series of commands that will continue to repeat over and over again untill a condition is met.
|

09-12-2011, 08:25 AM
|
 |
Contributing Member
Latest Blog: None
|
|
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
|
|
|
ameerulislam, scroll down to the "foreach() vs. for() vs. while(list() = each())" section of JohnnyS' link.
Is it relevant? Not really. Foreach and while are used for different things. The link Dan gave does explain the differences very well. Defrag is correct, but I think he's getting deeper than you want.
|

09-16-2011, 03:57 AM
|
|
Banned
Latest Blog: None
|
|
Join Date: 09-13-11
Posts: 36
|
|
|
While - loops through a block of code while a specified condition is true.
while (condition)
{
code to be executed;
}
do...while - loops through a block of code once, and then repeats the loop as long as a specified condition is true.
do
{
code to be executed;
}
while (condition);
For - loops through a block of code a specified number of times.
for (init; condition; increment)
{
code to be executed;
}
Foreach - loops through a block of code for each element in an array.
foreach ($array as $value)
{
code to be executed;
}
I hope that you can understand the difference.
|

09-16-2011, 09:20 AM
|
 |
Contributing Member
Latest Blog: None
|
|
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
|
|
|
@dataentryindia:
Foreach loops through an object. (An array is an object, but it loops through any object.)
|

09-19-2011, 03:42 AM
|
|
Banned
Latest Blog: None
|
|
Join Date: 09-13-11
Posts: 36
|
|
|
@Rukbat
Ok my mistake thanks to draw my attention on it.
|

09-19-2011, 10:11 PM
|
|
Banned
Latest Blog: None
|
|
Join Date: 08-30-11
Posts: 43
|
|
|
The while loop executes over and over again as long as the condition is true. The if statement only executes once.
|

09-30-2011, 04:39 AM
|
|
Junior Member
|
|
Join Date: 09-30-11
Posts: 1
|
|
Re: PHP Loops
Hello Everyone,
“Loops execute a block of code for specified number of times, or while a specified condition is true”. Often when you write code, you want to execute some line of code over and over again, to perform this task, use loops.
In PHP, we have the following looping statement;
1. while loop
2. Do…while loop
3. for loop
4. foreach loop
for more details please check out the following link...
http://mindstick.com/Articles/30ee50...9/?PHP%20Loops
Thanks !!!!!!
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 04:48 PM.
Powered by vBulletin Copyright © 2000-2013 Jelsoft Enterprises Limited.
Copyright © 2003 - 2013 Escalate Media LP
|
|
|