In code, you often have to use functions:

echo; 

and

 print; 

Which of the functions is heavier and heavier than the server?

  • what about how and how not to write - read about single and double quotes in php / I have a feeling that it would be useful - IFreet
  • @IFreet is a good answer to the question about print and echo :) - BomBom

6 answers 6

Indeed, we see that print may turn out to be a little slower than echo (or it may not be that the results of the first two methods demonstrate). At the same time, using echo with several parameters instead of concatenation (and single quotes instead of double quotes) gives a very noticeable performance gain.

You can read here on this topic.

    Repeated execution $ t0 = microtime (true); for ($ i = 0; $ i <10000; $ i ++) {echo "sdfsdfsdfsrn"; } echo microtime (true) - $ t0;

    and

     $t0 = microtime(true); for($i=0; $i<10000; $i++){ print "sdfsdfsdfs\r\n"; } echo microtime(true) - $t0; 

    Did not give visible gains of one or another option.

    Personal withdrawal; The speed is not in this. Often, everything is adjusted to work with data. Arrays files sockets, etc.

    • The speed in this is when 90% of the code is in double quotes, and so everything is written. then believe me, the results would be “pleasantly” surprising for you - Zowie
    • The string is intentionally enclosed in double quotes. You don’t know that the value of \ r \ n and how they work in 'and' I advise you to read the article the link to which was cited above habrahabr.ru/blogs/php/22951 - Alexander Banov
    • And what have \ r and \ n to quotes? - DemoS
    • While the result of the script will be different depending on the quotes - Alexander Banov
    • Do you think that I don `t know what is \ r \ n? <br> I’m forced to upset you, but you would google and read about performance tests, to put it mildly, it wouldn’t hurt --.- <br> You will immediately fall into place, I assure you - Zowie

    Most of the server load functions written by coders, who still do not know that saving on matches and premature optimization is the root of all evil.

    And yes, the OUT instruction will work the fastest.

    • Those. comrades, we will optimize strictly the database and caching, and if there is a huge load on the site, then we must fight for every match ... <br> The root of all evil is programmers who refuse to accept simple, <b> convenient </ b> rules motivating it anything but anything that is absolutely vseravno ... <br> <small> IMHO </ small> - Zowie
    • We will optimize strictly bottleneck, and if such is the database and the absence of caching, then yes, we will optimize them, not mythical quotes and synonymous functions. > huge load then you have to fight for every match ... Not a single highload project is fighting for every match. Far from it, highloads are struggling with optimizations on matches, because such optimizations are, at best, a waste of valuable developer time, and at worst, they are not readable and not amenable to code. Large loads are maintained due to the competent scalable architecture. - Ilya Pirogov
    • Do you know - when you need to call the same functions 100 times in 10 lines :) - you inadvertently start to wonder whether it is possible to reduce the load and make the script faster. - BomBom
    • IMHO a programmer who writes correctly should write correctly in everything and not just build the architecture correctly. - Zowie
    • @Fatahan, if you profile this code and it shows that echo / print is the bottleneck, and not, for example, a bad architecture leading to too frequent and inefficient calling of these functions, then yes, you can safely consider replacing echo with print optimization. In other cases - it saves on matches. - Ilya Pirogov

    echo is much faster than print . echo does not return any data, unlike print
    About double and single quotes in PHP read here.
    Everybody makes conclusions for himself

    You can also read here

    • print is also a language design, if that PS: why can not it be written that way? Well, what does not look very - it is. What is the main reason? - Alex Kapustin
    • print works as a function, echo just stupidly gives <br> By sabzh - we google and we read a lot about the calculation of strings in php and performance tests when using double and single quotes to avoid such silly questions -.- - Zowie
    • > echo "hello $ name"; // so it is impossible to write :) Since such a record is easier to write and easier to read, so you can and should write. In any case, programmers who estimate their development time are much more expensive than CPU time. - Ilya Pirogov
    • The second record is easier for me to read, and much more ... <br> You can immediately see where the static data and where the variables are ... <br> <b> the taste and color of the felt-tip pens are different </ b> - Zowie
    • > the taste and color of the markers are different. In this case, there was no reason why it was argued that it was impossible to write like that. - Ilya Pirogov

    And even better not as @AlexWindHope wrote

     echo 'привет ' . $name; 

    And so:

     echo 'привет ' , $name; 

    Because will be even faster, it is also in the article proposed by @Damon

    Well for the "unbelievers" spent tysty! The output of 2 variables on the screen through the gap. The number of output operations per one run is 10 thousand., The number of runs is 50, the average time for all half a million operations. Screen: image external link alt text

    • it's all very cool, but for some reason I have never seen a comma in echo in any source code ... <br> moreover - double quotes are used more often, and where they are not used - a dot is used ... - Zowie
    • AlexWindHope I use a single quote. - Palmervan
    • one
      Here is echo 'hi', $ name; = echo 'hi'; echo $ name; I do not think that it will work faster - no matter what they write there. - BomBom Sep.
    • one
      What are you all arguing about? Obviously, when using points in several receptions, a line is formed, each time in a new place. It is then passed to echo with a single argument. Commas form a list of arguments, essentially addresses of strings, which is passed to echo. No brainer that is faster. And they write with dots, not commas, apparently from certain aesthetic considerations and so that it looks like not some macros, but serious language. In short, fingers. - avp
    • one
      This is all savings on matches. - Alex Kapustin

    A single quote is not only faster, but also more convenient with html, then with an example:

     echo "<a href=\"link.html\">Link</a>"; echo '<a href="link.html">Link</a>'; 
    • not the best example, here you could just write not: <br> echo "<a href=\"link.html\"> Link </a>"; <br> a <br> echo "<a href = 'link .html '> Link </a> "; <br> <b>: p </ b> - Zowie
    • everyone has his own muddied) for this and there is universality and ability! - Palmervan
    • I agree with AlexWindHope - the code is simpler, readable and works without problems. - BomBom Sep. 2011