Is there a performance difference between "echo" of content and content in HTML?

11

Does using PHP echo to display any content on the screen differs in performance from using that same direct content in HTML?

For example, if I use a PHP file:

<?php echo "<p>Seja bem-vindo ao Stack Overflow!</p>";

Would it be different in performance to use in that same PHP file?

<p>Seja Bem vindo ao Stack Overflow!</p>

I could use both as one, as the content was static, I did not care much about using direct HTML.

Obviously this is a simple example, in the application the situation is different. I'm asking mainly because of page load time.

If there are other differences that you find relevant to complement the answer, put the same ones.

    
asked by anonymous 29.01.2016 / 18:15

3 answers

11

Pure HTML

Pure HTML is absurdly faster. It is a static file that does not need any processing, load PHP to process it, nothing. Shovel, blow, asked, sent.

Using PHP

If PHP is used to process the file, it depends on the amount of HTML text it will have to parse.

The documentation says using HTML is more efficient.

  

For outputting large blocks of text, dropping out of PHP parsing mode is generally more efficient than sending all of the text through echo or print

     

To write large blocks of text, getting out of parse mode of PHP is usually more efficient than sending all text through echo or print

This is because the parser goes on to work in a simplified scheme, understanding that it is only a text that it does not need to understand the meaning. When it is in a part of the text that it understands that it is PHP (by specific tag ), it is interpreting the code, and this is extremely more complicated to do and therefore slower.

I have not seen any tests and additional information, but the text implies that if the code blocks are small, parsing the parser context may slow it down this way and print it would be faster. Although I doubt it will make much difference.

Want to be sure? Test in your environment. And remember that what counts today may not count for tomorrow. And do not trust many of the results: P

I found a test if you want to have a base. But do not take it as absolute truth.

Premature optimization

As I always say, if performance affects, changing language is what should be the solution. PHP takes care of the message on almost everything, even having these differences. It is not important to analyze these small details to get performance, the gain will be very small.

Premature optimization is a bad . Not at all, but you have to know where to optimize. Staying to optimize everything you do is one of the worst things a developer can do. If all this optimization is required, PHP is not the proper language. And when you run after it, you have to recognize that you're using the golden hammer .

Ideal optimization

Of course the "ideal" would be a complete separation of what PHP is and what HTML is. If this is not possible, it would be good to minimize the use of PHP in a file that is to mount the layout of the page and avoid using HTML in PHP code.

I would not use PHP where it does not need, so in this simple example I would not give echo .

Conclusion

So you'd rather use HTML without going through PHP. If it does not, try using as few PHP as possible. If you need too much PHP and little HTML, then echo / print might be better, especially with single quotation marks.

You have to think about legibility, too. Mixing a lot is strange to read. Even if you have gained performance, it is not worth the loss.

Interesting reading .

    
29.01.2016 / 18:32
3

All content that is static, and does not need to be parsed in a language other than PHP, ASP, JSP, CGI, Ruby, dispatches as static content.

In the question example, the first code needs to be interpreted by the PHP compiler, so obviously there is a higher cost of time.

Evenifanoptimizationis"imperceptible", it is advisable to build the scripts as optimally as possible. But at this point we will enter into a discussion about performance, optimizations, good practices and all that mimimi that would make the answer out of focus and complex or something confusing and generalized too much. That's why I refrain from extending to this side.

    
30.01.2016 / 05:38
2

About page loading: does not change, what changes is on the server. The echo may slightly impact the rendering because it will execute an instruction that it would not need (I do not know how php handles this). but the user will receive the same content, with or without the use of echo.

    
29.01.2016 / 18:33