PHP needs a while-else statement

I think PHP ought to have a while-else statement. There is, of course, a while statement and an if-else statement, but a while() else statement would be perfect for returning a bunch of rows from a MySQL database:

<?php

$q = mysql_query("SELECT name FROM people WHERE age >= 21");
while (
$person = mysql_fetch_assoc($q))
{
    echo
"$person[name] is at least 21 years old.";
}
else
{
    echo
"No one 21 or older was found in the database!";
}

?>

This hypothetical code queries the database for everyone age 21 or older, looping through all the results. If no results were found, it very conveniently says so. Because there is no while-else statement, the programmer has to nest the the while() loop inside an if() statement. A while-else statement would be much cleaner.

17 thoughts on “PHP needs a while-else statement

  1. Interesting thought, I am inclined to agree with you, however I do not know what would go into making something like this happen, and I have no idea if it is even feasible. Perhaps someone with more knowledge on the subject will chime in.
  2. agreed, but alternative way is use one variable and if after while, for example

    $q = mysql_query(‘SELECT * FROM tbl’);
    $n = 0;
    while($r = mysql_fetch_assoc($q))
    {
    // do something
    ++$n
    }
    if(0 == $n)
    {
    // no rows found
    }

  3. Pingback: Richard K Miller dot coooooooooom » PHP 5 and Beyond

  4. I definitely agree. In fact, I was just trying to just that and upon that not working I googled it and realized it doesn’t exist. It’s a common sense statement, if you ask me (hence why I tried doing it). I reckon a post on php.net’s forum would be a good way of seeing why this cannot be done and if it can, give it some thought and perhaps enough traction to be implimented.
  5. I wholeheartedly agree, here’s something that I’ve considered using that represents the while-else. The only problem is if you change the list=fetch line, you have to do it twice.

    if (list($a,$b) = mysql_fetch_row($result)) {
    do {
    process($a,$b);
    } while (list($a,$b) = mysql_fetch_row($result));
    } else {
    print “No results returned”;
    }

  6. how do i do it here?

    while(!(connection_aborted() || connection_status() == 1) && $bytes_sent content_length)

    {

    //this sends data

    if(connection_aborted() || connection_status() == 0) {log_write(“Seems the client aborted the file download”); break;}

    }

  7. Woops, wrong move…like i was saying :
    A solution (simpler than taaniel’s one) is to put a flag :

    $hasresult=false;
    while($row=mysql_fetch_assoc($mysqlResult)){
    [do whatever you want]
    $hasresult=true;
    }
    if(!$hasresult){
    [do whatever you want]
    }

  8. I was just hoping for this functionality about 5 minutes ago. There is obvious workarounds, but this would be much much cleaner.
  9. I know hardly anything about website development and found myself needing a while-else statement. Great that we can Google and that I picked this suggested site. Thanks for the previous comments that suggest simple workarounds.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>