Edit report at https://bugs.php.net/bug.php?id=65181&edit=1

 ID:                 65181
 Updated by:         ni...@php.net
 Reported by:        ducciogrun at gmail dot com
 Summary:            Merging the "do {} while(cond)" and "while(cond){}"
                     structures
-Status:             Feedback
+Status:             Wont fix
 Type:               Feature/Change Request
 Package:            *General Issues
 Operating System:   Any
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

I'm sure that your IDE provides a way to disable this warning (though I do 
wonder why they included it in the first place, as this is a pretty standard 
idiom in PHP.)

If the expression grows too complicated, the most obvious solution is to just 
put the code in a separate method / function.

This do { ... } while (...) { ... } structure is not present in any other 
language that I'm aware of and is not particularly intelligible.


Previous Comments:
------------------------------------------------------------------------
[2013-07-02 12:36:24] ducciogrun at gmail dot com

@ab: Assignments in condition are not nice to read but one can live with them, 
with or without the explicit true or false check. 

But sometimes one has to do more complicated tasks (to name some dumb ones 
related to that short piece of code, maybe check if the connection to the 
database is still alive before fetching new data, check if there is enough 
memory available, update the log to inform that the process is about to fetch 
another row, or whatever).

Which one is more readable: 

while (false !== ((memory_get_usage(true)<MYSCRIPT_MAX_MEMORY) && mysql_ping() 
&& fwrite($log, "Start fetching a new row...") && $row = $result-
>fetch_assoc()));

or 

do {
    if(memory_get_usage(true)<MYSCRIPT_MAX_MEMORY) {
        /* write to log the reaching of max memory usage */
        break;
    }

    if(mysql_ping()) {
        /* reestablish connection and send again the query */
    }
    
    fwrite($log, "Start fetching a new row...");

    $row = $result->fetch_assoc();

} while ($row) {
  
    /* do what you have to do with the row */

}

Now, let's be honest, there are several ways to do that, for example to create 
a 
specific do_prefetch_jobs_and_return_row() function, nesting "if" and other 
structures,  but IMHO they are all less elegant :-)

I opened this feature request to see if I'm the only one that would like to see 
this construct implemented, before jumping directly on the Php source code

------------------------------------------------------------------------
[2013-07-02 08:45:05] a...@php.net

What about while (false !== ($row = $result->fetch_assoc())); ?

------------------------------------------------------------------------
[2013-07-02 08:35:45] ducciogrun at gmail dot com

Description:
------------
An example directly from the Php documentation 
(http://php.net/manual/en/function.mysql-fetch-assoc.php):

    /* fetch associative array */
    while ($row = $result->fetch_assoc()) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

It's old, it's prone to errors and my IDE (Zend Studio) always reports a 
"assignment in condition" warning. And many times thw work to be done on the 
first iteration is a lot more complicated than that.

One solution is to duplicate code:

    /* fetch associative array */
    $row = $result->fetch_assoc();
    while ($row) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
        $row = $result->fetch_assoc(); //Duplicate code
    }


But it is not the best, both to read and to mantain.

An elegant solution could be the following structure:

    /* fetch associative array */
    do {
        $row = $result->fetch_assoc();
    } while ($row) {
        printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
    }

Merging "do { } while(cond)" and "while(cond){}" would come handy anytime there 
is code that should be run at the first iteration and at any subsequent 
iteration, before the condition is checked.

The same purpose could be achieved by 

    /* fetch associative array */
    do {
        $row = $result->fetch_assoc();
        if($row) {
            printf ("%s (%s)\n", $row["Name"], $row["CountryCode"]);
        }
    } while ($row);

But it adds a level of nesting and it forces php to check twice the condition.



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=65181&edit=1

Reply via email to