Do while false in PHP?

·

I was working on something in Jetpack recently, and was mind-boggled when I came across a do…while( false ) loop.

Animated What Gif

What confused me so much, is why even use the loop if the condition was always false? Not able to figure out what this bit of code did, I brought the issue up to my team and Dan Walmsley was able to track it down.

Turns out, that in the initial commit, we were using the do...while( false )loop as something of a poor man’s go-to.

If you look at loop in the initial commit, you’ll see that there are several breakstatements. So, at any point in time, we could breakout of the loop and prevent any other code in the loop being reached. In effect, we would jumping to a spot just out of the loop hence, goto.

To do while false or not?

While I understand the construct now, it did confuse the hell out of me at first. For that reason alone, I’m not sure it should be used. If you are going to use it, I would suggest at least writing a comment so the next developer has an idea of what’s going on.

Comments

6 responses to “Do while false in PHP?”

  1. Pablo Avatar

    You’re wrong. GOTO allows you to go everywhere in the code. The statement do not!
    DO{…}WHILE(FALSE); simply let you exit the loop, exactly like a return in a function does.

    Have you ever had a lot of IF-ELSE nested used only to test the requirements?

    For example:
    $person = array();
    $person[‘lastname’] = sanitize($_POST[‘lastname’]);
    if($person[‘lastname’]==””)
    {
    $error = “Empty last name”;
    } else {
    if(strlen($person[‘lastname’])>30)
    {
    $error = “Last name is too long”;
    } else {
    $person[‘firstname’] = sanitize($_POST[‘firstname’];
    if($person[‘firstname’]==””)
    {
    $error = “First name is empty”;
    } else {
    //…do a lot of other suff
    }//end if firstname is empty
    }//end if lastname length
    }//end if firstname is empty

    It can be useful write a function like:
    function checkPerson()
    {
    $person = array();
    $person[‘lastname’] = sanitize($_POST[‘lastname’]);
    if($person[‘lastname’]==””) {
    return “Empty last name”;
    }//end if lastname is empty
    if(strlen($person[‘lastname’])>30) {
    return “Last name is too long”;
    }//end if lastname length
    $person[‘firstname’] = sanitize($_POST[‘firstname’];
    if($person[‘firstname’]==””) {
    return “First name is empty”;
    }//end if firstname is empty
    //…do a lot of other suff
    }
    Well… the function checks but also gathers and sanitizes the data. How do you get the valid data?

    Now look at this:
    $person = array();
    $error = “”;
    do {
    $person[‘lastname’] = sanitize($_POST[‘lastname’]);
    if($person[‘lastname’]==””)
    {
    $error = “Empty last name”;
    break;
    }//end if lastname is empty
    if(strlen($person[‘lastname’])>30)
    {
    $error = “Last name is too long”;
    break;
    }//end if lastname length
    $person[‘firstname’] = sanitize($_POST[‘firstname’];
    if($person[‘firstname’]==””)
    {
    $error = “First name is empty”;
    break;
    }//end if firstname is empty
    //…do a lot of other suff

    } while(false);

    Which list of IF-ELSEs is more readble? Which list of IF-ELSEs is easier to edit?
    Don’t you like DO-WHILE(ELSE) because you can read it? Just remember that in a DO-WHILE statement the code runs first and then condition is checked!
    Well, I suggest you other two ways to write it:
    FOR(;;;)){
    //do stuff
    BREAK;
    }
    SWITCH(TRUE)
    {
    DEFAULT:
    //do stuff
    BREAK;
    }

    1. Eric Binnion Avatar

      Youre wrong.

      Sure. That happens often. Though, I’m not 100% sure what your point is here.

      In your comment, it seems that you’re arguing that a do while false can be more readable than several if statements. I’m not sure I ever said that it couldn’t. What I did say is that it confused me at first. In my summary, I went on to say that, at a minimum, it probably makes sense to comment a do while false loop so that other developers could get up to speed quickly.

      1. Robin J Avatar

        I think in almost all cases, especially the one Pablo used in their example, the do-while (false) pattern can and should be refactored into a separate function. If code can be written in such a way that it doesn’t require unexpected usages of programming constructs, and therefore comments to explain said unexpected usages, then it probably should.

  2. Mr Alexander Avatar

    I am personally not a fan of this type of statement, instead I prefer to encapsulate the code either within another function, use goto: (if break is used);

    I think it is a little sloppy to use the do while false statement.

  3. Oli Avatar

    while(false) can only be used for this purpose. if confused, google it and arrive here

Leave a Reply to PabloCancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Eric Binnion

Subscribe now to keep reading and get access to the full archive.

Continue reading

Discover more from Eric Binnion

Subscribe now to keep reading and get access to the full archive.

Continue reading