While Loops

While loops are another useful construct to loop through data. While loops continue to loop until the expression in the while loop evaluates to false. A common use of the while loop is to return the rows in an array from a result set. The while loop continues to execute until all of the rows have been returned from the result set:

while($row = mysql_fetch_array($result)) { 
    // do something with the resulting row
}

You can also use the continue command to skip over the current iteration of the loop, then continue. For example:

while($row = mysql_fetch_array($result)) { 
    if($row['name'] ! = "Jet")) {
      continue;
    } else {
      // do something with the row
}

Get Advanced PHP for Web Professionals now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.