Re: Allow else after loop structures for,foreach and while?

From: Date: Sat, 15 Jul 2017 08:57:56 +0000
Subject: Re: Allow else after loop structures for,foreach and while?
References: 1  Groups: php.internals 
Request: Send a blank email to internals+get-99874@lists.php.net to get a copy of this message
Hi Michael,
Allow else blocks to follow iteration blocks. The else block is executed if no iteration occurs for any reason.
I also thought it would been a good idea, but most of the time i need a wrapper in the output like this:
    if($items) {
        echo '<ul>';
        foreach($items as $item) {
            echo '<li>', $item, '</li>';
        }
        echo '</ul>'
    }
And this may change the behavior of:
    if($result)
        foreach($items as $item)
            var_dump($item);
    else
        echo 'Nothing';
from:
    if($result) {
        foreach($items as $item) {
            var_dump($item);
        }
    }
    else {
        echo 'Nothing';
    }
to:
    if($result) {
        foreach($items as $item) {
            var_dump($item);
        }
        else {
            echo 'Nothing';
        }
    }
I believe this change is only possible (useful or not) if the curly braces are always required.

« previous php.internals (#99874) next »