Php: what's the difference between while...endwhile; and while { // stuff here }

There is no functional difference.

In practical use I find that:

while (expression):
// do stuff
endwhile;

Is more readable for the designers when you are embedding php code within html. IE:

<? while ($cssClass = array_pop($array)): ?>
   <li class="<?=$cssClass?>">
<? endwhile; ?>

Whereas:

while {

}

Is more readable within a php code block.


There's no difference, it comes down to personal preference.


There's no real difference when writing code.

There can be a difference in levels of convenience in very special circumstances. For example, suppose you are writing a template engine that converts template code to native PHP code which is then cached and executed directly for speed.

In this case, the fact that while...endwhile; avoids using braces may allow you to simplify your parsing algorithm if e.g. it recognizes variables that should be substituted with a syntax like {$var}, which also uses braces.

Of course this is a pretty small benefit in a really extraordinary situation, but you take what you can. :)

Tags:

Php