When creating any site, HTML code is necessarily used for building blocks and building a site in general.

And often this code is in a PHP file, since it makes no sense to put it into a separate file, and then include it again.

Suppose there is such a PHP file and there is such code in it:

<?php $title='Title'; $content='Content//'; echo <<<HTML <div class='title'>$title</div> <div class='content'>$content</div> HTML; 

But it can be derived more

 <?php $title='Title'; $content='Content//'; ?> <div class='title'><?echo $title; ?></div> <div class='content'><?echo $content; ?></div> 

Which way is better to use? Probably, the second method should be faster, but how much? Or you can still not think about it and write as convenient?

And what if through echo to output a lot of HTML code?

  • To output a large number of HTML code, it is better to use the second option. With large volumes, the performance will be very noticeable. - terantul
  • @terantul and why not use the second method always? - M11
  • You can always use. This is your right.) - terantul
  • @terantul there it’s clear that mine) I’m wondering which is better) - M11
  • one
    M11 was interested in the issue of performance optimization, unfortunately Smarti cannot boast with this. M11 - use the second option, it is better in performance. - terantul

5 answers 5

The test will be more eloquent than any disputes about performance:

 <?php $title='Title'; $content='Content//'; $start_a = microtime(true); for ($i = 0; $i<30000; ++$i) { echo <<<HTML <div class='title'>$title</div> <div class='content'>$content</div> HTML; } echo microtime(true) - $start_a; ?> 

0.597357988358

 <?php $title='Title'; $content='Content//'; $start_b = microtime(true); for ($i = 0; $i<30000; ++$i): ?> <div class='title'><?php echo $title; ?></div> <div class='content'><?php echo $content; ?></div> <?php endfor; echo microtime(true) - $start_b; ?> 

2.21032595634

  • Is the first way faster? - M11
  • Try it and see for yourself, I knowingly laid out the code for the test? Although I can not vouch for the correctness of such testing. Too little programming experience. For me personally, the second method is more convenient than the first, with rare exceptions. - VenZell
  • I launched the above code. 1 - variant 0.00045490264892578 2 - variant 0.0045950412750244 Hm. Wenzel bow. The first method is about 10 times faster. - terantul
  • @VenZell is just strange, because in the second method, php is used less, but echo is used 2 times. But still it can not kill the performance by about 4 times. - M11
  • one
    Yes, heredoc is a little faster, but the difference is just tiny. I do not know where I checked it, but I ran it on my VDS and the result is: // @ echo 0.033659934997559

use the HEREDOC syntax as you can ... uh ... don't use it at all. The second way is the right way. If you want to know the speed of the script, you can do so

  • But there are many factors. For example, in the second method it is called 2 times, and in the first one. - M11
  • 2
    The first way is a vivid example of light govnokod. See how CMS Opencart works. There, the templates are built like you have in the second method. The first method is inconvenient and if you want to look at the highlighting of your code, then inside HEREDOC you will not see anything except one solid color. I advise you not to use it at all. Maybe someone uses it, but I do not even understand why. - mountpoint
  • In the second case, the html code is given immediately to the client (browser) for processing, making short stops for inserting php variables into the html code. In the first case, first the php code with all html code is processed on the server. And this is not only time, but also server resources. In addition, if you insert a lot of html code into echo, its readability and editing speed will decrease significantly, I know from my own experience. - terantul
  • @mountpoint HEREDOC was used accidentally not for what exactly you can talk about it and so echo (""); - M11
  • @mountpoint, about the backlight - nonsense. Example from PHPStorm:! [Backlight in PHPStorm] [1] [1]: i.imgur.com/hTxlSMJ.png - VenZell

Better this way ;)

 <div class='title'><?= $title ?></div> <div class='content'><?= $content ?></div> 
  • 2
    no better =) short tags can be turned off and why additional problems when transferring the site?) that's the best way <div class = 'title'> <? php echo $ title; ?> </ div> - mountpoint
  • An interesting way, about this and did not know. Do you have any information about him? - M11
  • Php.ini directive php.net/manual/ru/ini.core.php#ini.short-open-tag short_open_tag boolean Determines whether the short form of writing (<??>) Of PHP tags is allowed. If you want to use PHP with XML, you can disable this option to use <? Xml?> Without difficulty. Otherwise, you can display this with PHP, for example: <? Php echo '<? Xml version = "1.0"?>'; ?>. If this option is disabled, you must use the long form of the opening PHP tag (<? Php?>). - terantul
  • 3
    Wait a minute In my opinion, gentlemen @mountpoint and @terantul confuse <? ?> and <? =?>. We read the manual : The short_open_tag directive also affected the abbreviation <? = To PHP 5.4.0, which is identical to the <? echo. Starting with PHP 5.4.0, the <? = Entry is always available. So use without fear. - Denis Khvorostin
  • 2
    I will take my words back when at least 90% of hosters put PHP 5.4.0 by default - mountpoint

I always write this way, it works on all hosting with php 5.3 regardless of whether short tags are included

 <div class='title'><?= $title ?></div> <div class='content'><?= $content ?></div> 
  • @Valentin Zhukov, your answer duplicates the answer of @Stan. - VenZell
  • I have no opportunity to comment, he wrote everything correctly, just wanted to add that this is no short tags - Valentin Zhukov
  • one
    it's just that I have a bunch of pipetts as it tears, they make up any govnogoda with their template engines, they also write smarts, this is cool and this code will fall off, Nehru would not fall off would try first before writing any nonsense - Valentin Zhukov

If you need to optimize so much, it is better to optimize not the code, but the infrastructure. Options:

  • Write not in PHP, but in Go / Java / C
  • More optimally configure php-fpm, nginx, ... (output buffering, gzip, ...)
  • Use a faster server or multiple servers with load balancer
  • Cache entire pages or at least blocks - Caching with Varnish + ESI (RuHighload.com)