Last Updated: February 25, 2016
·
712
· ekrembk

When to use alternative syntax

Please, please use alternative syntax when mixing HTML & PHP.

This is ugly:

<?php if( some_function() ) { ?>
    Lorem ipsum
<?php } else { ?>
    <?php foreach( $some_array as $foo ) {
        Dolar sit amet
    <?php } ?>

    Consectetur adipisicing
<?php } ?>

Most IDE's will not highlight the braces when mixing and this is really not clear.

Better solution:

<?php if( some_function() ):?>
    Lorem ipsum
<?php else:?>
    <?php foreach( $some_array as $foo ):?>
        Dolar sit amet
    <?php endforeach;?>

   Consectetur adipisicing
<?php endif;?> 

1 Response
Add your response

the "endif" keyword makes me shutter in remembrance of my vb days.

over 1 year ago ·