SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict shad0w's Avatar
    Join Date
    Aug 2003
    Location
    PA
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    foreach(); vs. while();

    I've always used a while loop to spit out my mysql results, but as I become more active on the forums, I've been seeing people use the foreach() loop instead. I looked through the manual and the two look almost identicle except for a few syntactical things, so what's the difference? Is one faster than the other or what?

  2. #2
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    while and foreach don't really serve the same purpose.

    while performs the loop as long as the condition is true, foreach loops through an array. while can also be used to loop through an array, but they serve 2 different purposes.

    maybe some example code could help out.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.

  3. #3
    SitePoint Addict shad0w's Avatar
    Join Date
    Aug 2003
    Location
    PA
    Posts
    239
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Lange
    maybe some example code could help out.
    Yes it probably would because I only really use while for looping through arrays anyway.

  4. #4
    "Of" != "Have" bronze trophy Jeff Lange's Avatar
    Join Date
    Jan 2003
    Location
    Calgary, Canada
    Posts
    2,063
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, well, umm:

    PHP Code:
    $array = array(=> 'a'=> 'b'=> 'c'=> 'd''e');
    foreach (
    $array as $number => $letter// Loops through every array value
        
    echo $number $letter;
    while (list(
    $number$letter) = each($array)) // Loops through every array value
        
    echo $number $letter
    Basically those examples do the exact same thing. Now onto other while statements:

    PHP Code:
    while ($var 100)
        
    $var++; 
    Basically that performs the loop for as long as the condition is true, which happens to be as long as $var is less than 100.

    Now moving onto something like a MySQL Fetched Array, after performing a mysql_query(), the results are sent back to PHP, which it stores as a dataset in the memory, which has an internal pointer pointing to which row it is currently on. When a call to mysql_fetch_array() is made, it returns the current row as an array, and moves the pointer to the next row. If the pointer is at the end of a result-set, mysql_fetch_array() returns false. Meaning, when doing the following:

    PHP Code:
    while ($row mysql_fetch_array($query)) 
    The while statment will run until the condition returns a false, which will happen once the internal pointer reaches the final row in the result-set.


    I hope that made some sort of sense.
    Who walks the stairs without a care
    It shoots so high in the sky.
    Bounce up and down just like a clown.
    Everyone knows its Slinky.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •