WEB While() loop expression

Discussion in 'OT Technology' started by nubian, Mar 8, 2009.

  1. nubian

    nubian Active Member

    Joined:
    Aug 11, 2003
    Messages:
    27,761
    Likes Received:
    1
    though i have a general understanding of how while loops works.
    i just don't understand how it works here.

    Code:
    <?php
    $array = array(1,2,3);
    
    $value = end($array);
    while ($value){
    	echo "$value<br />";
    	$value = prev($array);
    }
    ?>
    how is it that while ($value) holds true till it's false from the $array array?

    thanks
     
  2. fishbulb

    fishbulb Active Member

    Joined:
    Oct 29, 2001
    Messages:
    6,848
    Likes Received:
    4
    Location:
    md
    $value = end($array);

    sets $value to the last item in the array, in this case 3

    then the while loop starts ( while what is between the parenthesis holds true, so in this case until the $value is equal to FALSE. )
    $value is set to prev, 2.
    loop again, $value set to 1.
    loop again, prev attempts, no previous value is found in the array, so $value is set to false ( see here for explanation: http://us2.php.net/prev )
    $value is now FALSE, so the loop ends.
     
  3. nubian

    nubian Active Member

    Joined:
    Aug 11, 2003
    Messages:
    27,761
    Likes Received:
    1
    awesome thanks for putting it into perspective :bowdown:
     

Share This Page