Yeah you're right, that's way better.



  •  The following is from here.  Emphasis on the excellent alternative to a perfectly normal control structure suggested in the last line (after the code snippet).

    Advanced C users may be familiar with a different usage of the do-while loop, to allow stopping execution in the middle of code blocks, by encapsulating them with do-while (0), and using the break statement. The following code fragment demonstrates this:

    <?php
    do {
        if (
    $i 5) {
            echo 
    "i is not big enough";
            break;
        }
        
    $i *= $factor;
        if (
    $i $minimum_limit) {
            break;
        }
       echo 
    "i is ok";

        
    /* process i */

    } while (0);
    ?>

    Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'. Since PHP 5.3.0, it is possible to use goto operator instead of this hack.



  • Link is wrong



  • @sys said:

     The following is from here.  Emphasis on the excellent alternative to a perfectly normal control structure suggested in the last line (after the code snippet).

    Advanced C users may be familiar with a different usage of the do-while loop, to allow stopping execution in the middle of code blocks, by encapsulating them with do-while (0), and using the break statement. The following code fragment demonstrates this:

    <?php
    do {
        if (
    $i 5) {
            echo 
    "i is not big enough";
            break;
        }
        
    $i *= $factor;
        if (
    $i $minimum_limit) {
            break;
        }
       echo 
    "i is ok";

        
    /* process i */

    } while (0);
    ?>

    Don't worry if you don't understand this right away or at all. You can code scripts and even powerful scripts without using this 'feature'. Since PHP 5.3.0, it is possible to use goto operator instead of this hack.

    ...Why? Is else really that evil?

    I mean, I guess I can kind of see this if you are worried about nesting conditionals a lot...but why wouldn't you use a validation function and returns (ooh, evil multiple exit points!)?



  • @sys said:

    Advanced C users may be familiar with a different usage of the do-while loop, to allow stopping execution in the middle of code blocks, by encapsulating them with do-while (0), and using the break statement.


    Proper advanced C users will probably use sane code... Wait, that's an oxymoron, isn't it?


    @toth said:
    Why? Is else really that evil?

    I mean, I guess I can kind of see this if you are worried about nesting conditionals a lot...but why wouldn't you use a validation function and returns (ooh, evil multiple exit points!)?

    Agreed. This example is a perfect example of a totally unnecessary hack:
    <?php
        if ($i 5) {
            echo 
    "i is not big enough";
        } else {
            
    $i *= $factor;
            if (
    $i >= $minimum_limit) {
                echo 
    "i is ok";
                
    /* process i */
            
    }
        }
    ?>

    Bonus: the program flow is much easier to comprehend like this.


  • I've seen this same kind of construct in MCF, an application in the SPEC benchmark suite (a suite widely used for benchmarking in computer engineering/science research).

    I guess if you're making a suite representative of real applications you need to represent stupidity as well.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.