From:             ducciogrun at gmail dot com
Operating system: Any
PHP version:      Irrelevant
Package:          *General Issues
Bug Type:         Feature/Change Request
Bug description:Merging the "do {} while(cond)" and "while(cond){}" structures

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 bug report at https://bugs.php.net/bug.php?id=65181&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65181&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65181&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65181&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65181&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65181&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65181&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65181&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65181&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65181&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65181&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65181&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65181&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65181&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65181&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65181&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65181&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65181&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65181&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65181&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65181&r=mysqlcfg

Reply via email to