3

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

kadamb
  • 163
  • 1
  • 1
  • 7
  • 1
    Better would be to no inject HTML via PHP... Use a proper framework to this for you. https://softwareengineering.stackexchange.com/a/180504/211576 – RvdK Sep 21 '17 at 12:26
  • Ideally you wouldn't be constructing your pages this way, because it's an antipattern. However if you must pick between the two, historically I've always seen the first done, though I don't know the reasons behind it. – Neil Sep 21 '17 at 12:41
  • @RvdK : thanks for pointing me to correct answers – kadamb Sep 21 '17 at 12:42
  • 1
    Possible duplicate of [Is micro-optimisation important when coding?](https://softwareengineering.stackexchange.com/questions/99445/is-micro-optimisation-important-when-coding) – gnat Sep 21 '17 at 14:13
  • 2
    Possible duplicate of [Which is better: to include HTML inside PHP code or outside it?](https://softwareengineering.stackexchange.com/questions/180501/which-is-better-to-include-html-inside-php-code-or-outside-it) – HorusKol Sep 22 '17 at 04:59

2 Answers2

2

As always, the short and probably most correct answer: It depends.

To expand:

For the two snippets in that short as shown in the question it's purely about taste. There is no strong technical reason for one over the other. In a larger context the question is "are those rather HTML templates with a little logic in between or are those code parts with lot's of logic and a little of HTML?"

Of course if it's mostly logic, a good idea is to look into template library's like Twig or others and separate the logic from output in a larger degree, whcih allows testing and changing output formats more easily.

johannes
  • 3,601
  • 2
  • 26
  • 32
  • It is not recommended to echo HTML using PHP, due to performance. So even in this situation the first approach would be the preferred solution. – Andy Sep 22 '17 at 10:14
  • 1
    If you care that much about performance don't use PHP, but native code. For at least 99% of these cases readability and maintainability of the code is more relevant than a tiny potential performance difference (which can change from PHP version to PHP version and where I'm too lazy to think about as it's irrelevant) – johannes Sep 22 '17 at 11:14
2

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.

Mazaryk
  • 135
  • 3