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

Asked

Viewed 307 times

12

The use of echo PHP to display on the screen any content differs in performance to use this same content directly in HTML?

For example, if I use a PHP file:

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

It would be different in performance to use in this same PHP file?

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

I could use both, as the content was static, I didn’t care much about using direct HTML.

It is obvious that this is a simple example, and in the application the situation is different. I am asking mainly due to page loading time.

If there are other differences that you find complementary to the answer, put them.

  • 3

    The second is much faster by not using php :P if the file is . html.

  • rray even imagined this? but why? php comes to be heavy, consumes a lot or something like?

  • Diegodesouzasilva what @rray said is if you don’t go by php :) And there’s also the issue of double quotes being slower (Dit, this should be true, but PHP does a shaving with escape in single quote, which is slow too).

  • You got that yet? I didn’t know that one...

  • 1

    @Diegodesouzasilva double quotes PHP searches for variables to replace. Always use the simple ones, if it is plain text, then PHP does not have to parse the content (Dit: in an ideal world. the current implementation tb screws in simplespor quote because of the escape). Same thing in HEREDOC and NOWDOC.

  • 1

    @I think sometimes worrying about this "faster" is bullshit. If you’re using a framework (or even if you haven’t configured your php.ini), output buffer will store everything in memory to send it to the browser only at the end.

  • If the text is fixed in a file html php is not even processed, it is only time for the server to find the requested file and return it to the client. I think folks liked the first part of the sentence by php being an interpreted language so it doesn’t have a good performance when compared to compiled languages.

  • Good let me see if I understand, so if I use two php files, one only using echo to print x amount of content and another php file using html to display that same amount of content, the html file will load faster always (even if by thousandths, hundredths, etc.) ?

Show 3 more comments

3 answers

12


Pure HTML

Pure HTML is absurdly faster. It’s a static file that doesn’t need any processing, load PHP to process it, nothing. Boy, boy, he asked, he sent.

Using PHP

If PHP is used to process the file, it depends on how much HTML text it has to parse, but the fact that it has a minimum is already absurdly slower.

To 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, exit from the parse PHP is generally more efficient than sending all text through a echo or print

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

I have not seen tests and additional information, but the text suggests that if the code blocks are small, the context exchange of the parser can make it slower to do so and the print would be faster. Although I doubt it will make much difference.

Want to be sure? Test your environment. And remember that what is worth today may not be worth tomorrow. And don’t rely too much on the results :P

I found a test if you want to have a basis, but don’t take it as absolute truth.

Premature optimization

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

Premature optimization is an evil. Not at all, but you have to know where to optimize. Looking to optimize everything you do is one of the worst things a developer can do. If all this optimization is necessary, PHP is not the proper language. And when one runs after this one has to recognize that one is using the golden hammer.

Optimal optimization

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

I wouldn’t use PHP where you don’t need it, so in this simple example I wouldn’t give echo.

Completion

So prefer to use HTML without going through PHP. If you don’t, try using as little PHP as possible. If you need too much PHP and too little HTML, then echo/print can be better, especially with simple quotes.

You have to think about readability too, mixing too much gets weird to read, even if you have gained performance is not worth the loss.

Interesting reading.

4

All content that is static, and does not need to parse in another language independent of PHP, ASP, JSP, CGI, Ruby, dispatch as static content.

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

inserir a descrição da imagem aqui

Even if an optimization is "imperceptible", it is recommended to build the scripts as optimally as possible. But at this point we’re going to get into a discussion about performance, optimizations, good practices and all that mimimi that would make the response out of focus and complex or something too confusing and generalized. So I refrain from extending to that side.

  • Your illustration is very good and explanation too!

2

About page loading: does not change, what changes is on the server. echo can slightly impact the rendering, as it will execute an instruction that it would not need (I don’t know how php treats it). but the user will receive the same content, with or without using echo.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.