Alternative syntax in PHP

In PHP, there are a lot of alternative syntax. It can improve the legbility of your code.


if, while, for, foreach, switch

The alternatice syntax in control structures is to remove the openning brace instead of a colon and closing with endif, endwhile, endfor, foreach, and endswitch.

if, endif

<?php if ($a == 5): ?>
A is equal to 5
<?php endif; ?>

while, endwhile

<?php while ($a < 5): ?>
<?php print $a++; ?>
<?php endwhile; ?>

Output:

01234

Remember, do not use any indent in this case.

Read more...