Webmaster Forum


Go Back   Webmaster Forum > Web Development > Coding Forum

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


Reply
 
LinkBack Thread Tools Display Modes
Share |
  #1 (permalink)  
Old 09-05-2011, 01:03 AM
No Longer Active
Latest Blog:
None

 
Join Date: 12-30-10
Location: Dhaka
Posts: 1,350
iTrader: 0 / 0%
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?
 
Reply With Quote
  #2 (permalink)  
Old 09-05-2011, 01:28 AM
The Controversial Coder
Latest Blog:
None

 
Join Date: 05-01-06
Location: Manchester; UK
Posts: 2,376
iTrader: 0 / 0%
Undoubtedly this will explain the differentiation far more eloquently than I could.
 
Reply With Quote
  #3 (permalink)  
Old 09-05-2011, 03:10 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
Quote:
Originally Posted by ameerulislam View Post
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..
 
Reply With Quote
  #4 (permalink)  
Old 09-05-2011, 06:11 AM
Contributing Member
Latest Blog:
None

 
Join Date: 08-15-11
Posts: 50
iTrader: 0 / 0%
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.
__________________
Learn how to photoshop with these Photoshop Tutorials!
 
Reply With Quote
  #5 (permalink)  
Old 09-05-2011, 06:27 AM
No Longer Active
Latest Blog:
None

 
Join Date: 12-30-10
Location: Dhaka
Posts: 1,350
iTrader: 0 / 0%
Quote:
Originally Posted by JohnnyS View Post
they differ in speed and performance..
Is the link you gave relevant? coz it's the root domain. Your link is not opening here anyways.
 
Reply With Quote
  #6 (permalink)  
Old 09-12-2011, 02:22 AM
JohnnyS's Avatar
Contributing Member
 
Join Date: 07-05-11
Location: philippines
Posts: 312
iTrader: 0 / 0%
Quote:
Originally Posted by ameerulislam View Post
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/
 
Reply With Quote
  #7 (permalink)  
Old 09-12-2011, 02:44 AM
Banned
Latest Blog:
None

 
Join Date: 07-13-11
Location: In Fr0nt 0f C0mputer
Posts: 132
iTrader: 3 / 100%
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.
 
Reply With Quote
  #8 (permalink)  
Old 09-12-2011, 08:25 AM
Rukbat's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
iTrader: 0 / 0%
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.
 
Reply With Quote
  #9 (permalink)  
Old 09-16-2011, 03:57 AM
Banned
Latest Blog:
None

 
Join Date: 09-13-11
Posts: 36
iTrader: 0 / 0%
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.
 
Reply With Quote
  #10 (permalink)  
Old 09-16-2011, 09:20 AM
Rukbat's Avatar
Contributing Member
Latest Blog:
None

 
Join Date: 08-08-11
Location: Long Island, NY, USA
Posts: 287
iTrader: 0 / 0%
@dataentryindia:

Foreach loops through an object. (An array is an object, but it loops through any object.)
 
Reply With Quote
  #11 (permalink)  
Old 09-19-2011, 03:42 AM
Banned
Latest Blog:
None

 
Join Date: 09-13-11
Posts: 36
iTrader: 0 / 0%
@Rukbat


Ok my mistake thanks to draw my attention on it.
 
Reply With Quote
  #12 (permalink)  
Old 09-19-2011, 10:11 PM
Banned
Latest Blog:
None

 
Join Date: 08-30-11
Posts: 43
iTrader: 0 / 0%
The while loop executes over and over again as long as the condition is true. The if statement only executes once.
 
Reply With Quote
  #13 (permalink)  
Old 09-30-2011, 04:39 AM
Junior Member
 
Join Date: 09-30-11
Posts: 1
iTrader: 0 / 0%
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 !!!!!!
 
Reply With Quote
Go Back   Webmaster Forum > Web Development > Coding Forum

Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference please? genius007 SEO Forum 5 02-19-2011 12:12 PM
Loops jdspc Forum Lobby 7 06-24-2008 01:16 AM
Table cell borders and video loops lajones3 Coding Forum 5 08-24-2007 12:57 PM
What is (are) the difference (s)... kos Forum Lobby 6 08-02-2007 02:41 AM


V7N Network
Get exposure! V7N I Love Photography V7N SEO Blog V7N Directory


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




Search Engine Optimization by vBSEO 3.6.0 RC 2 ©2011, Crawlability, Inc.