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.
The second is much faster by not using php :P if the file is . html.
– rray
rray even imagined this? but why? php comes to be heavy, consumes a lot or something like?
– DiChrist
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).
– Bacco
You got that yet? I didn’t know that one...
– DiChrist
@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.
– Bacco
@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.
– Wallace Maxters
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.– rray
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.) ?
– DiChrist