Current location: Hot Scripts Forums » Programming Languages » PHP » foreach or while loops


foreach or while loops

Reply
  #1 (permalink)  
Old 03-22-04, 07:44 AM
clintre clintre is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
foreach or while loops

Which do you recommend and why?

I have used both to do essentially the same thing when pulling results from a database. I looked it up on php.net and there seems to be a split over which is better to use. So I just wanted to get some opinions here.
Reply With Quote
  #2 (permalink)  
Old 03-22-04, 07:49 AM
MrStocks MrStocks is offline
Newbie Coder
 
Join Date: Mar 2004
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Foreach works specialy for arrays:
foreach($myArray as $value) { }

While something is true, also while will re-calculate the data everytime.

We do not use them in the same contexts (true they can be used)
Reply With Quote
  #3 (permalink)  
Old 03-22-04, 07:54 AM
clintre clintre is offline
Newbie Coder
 
Join Date: Aug 2003
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
I tend to use foreach most of the time myself because it seems to make more sense for arrays. I thought it was the best way, but I got into an argument with my friend that said I should be using while loops for everything.
Reply With Quote
  #4 (permalink)  
Old 03-22-04, 09:17 AM
NeverMind's Avatar
NeverMind NeverMind is offline
Community VIP
 
Join Date: Aug 2003
Location: K.S.A
Posts: 2,257
Thanks: 0
Thanked 2 Times in 1 Post
foreach is the best when dealing with arrays IMO..

while is good when you are dealing with DB results ..
__________________
PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
Reply With Quote
  #5 (permalink)  
Old 03-22-04, 07:08 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by NeverMind
foreach is the best when dealing with arrays IMO..

while is good when you are dealing with DB results ..
As NeverMind said, foreach is best when dealing with arrays... but they are also best for doing thing with math (repeating thing values to how many time to display it, or skip stuff in between and etc) while, a while loop is just one standard thing..you can't do much with it.
__________________
New beta project: GFX-PRO.ATH.CX
Description: GFX-PRO a great new project that anyone can and setup in less then a minute. [ More info ]
Website: http://gfx-pro.ath.cx
Status: Online
http://gfx-pro.ath.cx/sig.png
Reply With Quote
  #6 (permalink)  
Old 03-22-04, 09:23 PM
blaw's Avatar
blaw blaw is offline
Junior Code Guru
 
Join Date: Dec 2003
Location: Vancouver, BC, Canada
Posts: 550
Thanks: 0
Thanked 0 Times in 0 Posts
Hi there,

To add to you guys, especially, when the array is an associative array, foreach() does a terrific job:

PHP Code:

foreach ($myArray as $key => $val) {

      echo 
'Key ('.$key.') contains Value: '.$val;

However, though, it's really up to what you are doing. Like some said, when dealing with database resource, while() + fetch() is a good combination or when you don't want the loop to look at "each" of whatever you're looping, then you shouldn't use foreach().

My little comment. =)
__________________
Blavv =|
Reply With Quote
  #7 (permalink)  
Old 03-22-04, 09:50 PM
Infinite_Hackers's Avatar
Infinite_Hackers Infinite_Hackers is offline
Coding Addict
 
Join Date: Dec 2003
Posts: 307
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by Infinite_Hackers
As NeverMind said, foreach is best when dealing with arrays... but they are also best for doing thing with math (repeating thing values to how many time to display it, or skip stuff in between and etc) while, a while loop is just one standard thing..you can't do much with it.
Btw, i was taking about the for() loop, not the foreach lol :S (just got a bit mixed up)
__________________
New beta project: GFX-PRO.ATH.CX
Description: GFX-PRO a great new project that anyone can and setup in less then a minute. [ More info ]
Website: http://gfx-pro.ath.cx
Status: Online
http://gfx-pro.ath.cx/sig.png
Reply With Quote
Reply

Bookmarks


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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Classified Ads skipper23 Perl 3 11-22-05 02:22 AM
formmail problem gscraper Perl 12 08-27-04 03:06 AM
Formmail Script Fix - Cant display multiple list selections.. zamen PHP 3 05-02-04 09:59 AM
Classified Ads skipper23 Perl 2 12-30-03 03:43 AM
Branching, Loops and Subroutines HELP NEEDED Jerome Visual Basic 0 11-30-03 09:55 AM


All times are GMT -5. The time now is 10:20 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.