PHP HTML Printing – Better Way to Print HTML in If-Else Conditions

conditionshtmlPHP

Please refer the following two forms of the same code

First:

<?php if(some_condition){
?>
    <li><a target="_blank" href="example.com/channel1/"  class="xyz">If content</a></li>
<?php
}else{
?>
    <li><a target="_blank" href="example.com/channel2/"  class="xyz">Else Content</a></li>

<?php?>
}?>

Second :

<?php 
    if(some_condition){
       echo '<li><a target="_blank" href="example.com/channel1/"  class="xyz">If content</a></li>';
    }
    else{
        echo '<li><a target="_blank" href="example.com/channel2/"  class="xyz">Else Content</a></li>';
    }
 ?>

My question is which one of the following is a better option.I know the second one is more readable. But if there are so many conditions on the same page, then which is better performance-wise(i believe, there will be only a negligible performance difference between the two).

I would like to know your views/points, regarding standards performance and whatever you think I should know, about the two different forms.

(And which should be preferred over the other,when there is only single if-else block, and multiple if-else blocks)

Thanks

Best Answer

I would argue that the first example is the better of the two evils as it is more maintainable. The HTML is written as is, and not coded as a string literal.

Using templating or some other way to keep business logic and HTML presentation separate usually results in more maintainable code.