Home php Text output: use echo or not?

Text output: use echo or not?

Author

Date

Category

When creating any site, the HTML code is used to build blocks and in general site construction.

and often this code is in the PHP file, as it makes no sense to endure it in a separate file, and then still incorrect.

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

& lt;? php
$ title = 'title';
$ CONTENT = 'CONTENT //';
ECHO & LT; & LT; & LT; HTML
& lt; div class = 'title' & gt; $ Title & lt; / div & gt;
& lt; div class = 'content' & gt; $ content & lt; / div & gt;
HTML;

but it can be removed yet so

& lt;? php
  $ title = 'title';
  $ CONTENT = 'CONTENT //';
? & gt;
& lt; div class = 'title' & gt; & lt;? echo $ title; ? & gt; & lt; / div & gt;
& lt; div class = 'content' & gt; & lt;? Echo $ Content; ? & gt; & lt; / div & gt;

What is the best way to use? Probably the second way should be faster, but much? Or you can still not think about it and write as more convenient?

And what if via ECHO output a lot of HTML code?


Answer 1, Authority 100%

The test will be eloquent any disputes about performance:

& lt;? php
$ title = 'title';
$ CONTENT = 'CONTENT //';
$ Start_a = Microtime (True);
For ($ i = 0; $ i & lt; 30000; ++ $ i) {
ECHO & LT; & LT; & LT; HTML
& lt; div class = 'title' & gt; $ Title & lt; / div & gt;
& lt; div class = 'content' & gt; $ content & lt; / div & gt;
HTML;
}
Echo Microtime (True) - $ Start_A;
? & gt;

0.597357988358

& lt;? php
$ title = 'title';
$ CONTENT = 'CONTENT //';
$ start_b = microtime (true);
FOR ($ i = 0; $ i & lt; 30000; ++ $ i) :? & gt;
& lt; div class = 'title' & gt; & lt;? php echo $ title; ? & gt; & lt; / div & gt;
& lt; div class = 'content' & gt; & lt;? php echo $ content; ? & gt; & lt; / div & gt;
& lt;? php endfor;
Echo Microtime (True) - $ Start_B;
? & gt;

2.21032595634


Answer 2, Authority 23%

Use Heredoc Syntax as you can … EE .. Do not use at all. The second method is the right way. If you want to know the speed of the script, then you can make so


Answer 3, Authority 15%

better so;)

& lt; div class = 'title' & gt; & lt;? = $ Title? & gt; & lt; / div & gt;
& lt; div class = 'content' & gt; & lt;? = $ Content? & gt; & lt; / div & gt;

Answer 4, Authority 8%

Always write so, works on all hosting with PHP 5.3 in Independence Are the shorts tags included

& lt; div class = 'title' & gt; & lt;? = $ Title? & gt; & lt; / div & gt;
& lt; div class = 'content' & gt; & lt;? = $ Content? & gt; & lt; / div & gt;

Answer 5

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

  • write not on PHP, but on go / java / c
  • more optimally configure php-fpm, nginx, … (output buffering, gzip, …)
  • Use a faster server or multiple servers with Load Balancer
  • cache pages whole or at least blocks – caching with Varnish + ESI (ruhighload.com)

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions