'echo' or abandon "programming" to write HTML, and then run the PHP code again

For the most part, when I want to display some kind of HTML that will actually display, I would use the "close PHP" tag, write HTML, and then open PHP again. eg,

<?php // some php code ?> <p>HTML that I want displayed</p> <?php // more php code ?> 

But I saw a lot of people who would just use an echo instead, so they would do something like

 <?php // some php code echo("<p>HTML that I want displayed</p>"); // more php code ?> 

Is any performance good for falling and vice versa? I would suggest that this is not the way the PHP engine should handle the entire file anyway.

How about when you use the echo function so that the dose does not look like a function, for example

 echo "<p>HTML that I want displayed</p>" 

I would hope that this is purely a matter of taste, but I would like to know that I am missing something. I personally find the first method to be preferable (crowding out PHP and then back), because it helps to make a clear distinction between PHP and HTML, and also allows you to use code highlighting and hint at your HTML, which is always convenient.

+7
performance php echo
source share
4 answers

The first type is preferable, for the reasons indicated.

Actually, repeating whole html fragments is considered bad practice.

+6
source share

No, there is no performance increase that would be visible.

Sometimes it’s just easier to output content using an echo (for example, when inside or in a loop) than closing the php tag.

+1
source share

I think there is a preprocessor that converts the same form to the second. One way or another, what happens in ASP.NET. And in both ASP.NET and classic ASP, loops can stretch across raw-HTML regions.

0
source share

There is no difference in performance.

Only the style that creates the most readable code. Depending on the real situation, which may be one of two.

But you should still avoid mixing HTML and PHP. This can be done using the template system for your views.

0
source share

All Articles