This is micro-optimization, does not affect at all times, there are things we do in our codes beyond this, like create huge classes or include classes and functions without need that these yes may have performance problems for the final script.
Concatenating strings will rarely make any impact, only on one "stress test" well over that will feel some variation and yet it will be minimal what is not worth taking the trouble.
Why echo wears comma
The reason for echo
use ,
it is not for performance reasons, either that on a machine or php version in a very heavy stress test echo 'a'.'b';
can eventually be better than echo 'a', 'b';
and the next test on the same machine may not, it will depend on the timing, the same reason is to facilitate the use other things like doing a mathematical operation, see the difference:
<?php
echo '0', 5 + 2, '3', PHP_EOL; //Resulta em 073
echo '0' . 5 + 2 . '3', PHP_EOL; //Resulta em 73
Then note that the zero ended up being "confused" with part of the mathematical operation in the second echo
This is just one example, there are many more situations where using the comma will be much more practical and may make it easier for you.
Concluding
About your question
In a situation where you receive millions of requests on the server all the time, then you need to get the maximum server performance, which would be the best way to display different data in the same function?
Answering the question something like this echo 'foo' . $bar;
against echo 'foo', $bar;
there will be no difference at all perseptive, what can affect and what usually affects in this issue may be numerous other things, as things that may often perhaps be unnecessary (which should only happen sometimes, but you end up doing perform almost always):
- Giant frameworks for simple things like using Laravel or cakephp to create a simple 3-page website
- Executions of "requests", how to access in a database and do nothing, not even a
SELECT
or UPDATE
- Start functions without need or on page that are not required, such as starting a
session_start
on a page that does not require a session
There are more things can affect, but it is quite relative
How to improve the performance of my scripts
Outside you avoid the situations I commented above, another interesting thing to use on producing (do not use in development environments) is Opcache or Xcache:
PHP has nothing like JIT (Just In Time) natively, however there is the extension Opcache which can be enabled on PHP5.6+ servers (for earlier versions you have to install manually or via Pearl and not all servers allow this), PHP is an interpreted language, without 1000 people request a page PHP will reinterpret the scripts for that request 1000 each and then run them, but with Opcache (or other alternatives like Xcache) to interpretation of the code is in a type of "cache", ie is interpreted "once" (approximately) and stays in "cache" for some time, so the next requests will only run.
Opcache (or others) at least in tests that I have done greatly improve, even in a test with Apachebench (tool to simulate multiple simultaneous requests) greatly improved the response time, I executed the following command:
Sem Opcache:
Requests per second: 2176.80 [#/sec] (mean)
With Opcache:
Requests per second: 2350.93 [#/sec] (mean)
It seems the number hasn’t changed, but do the math 2350 - 2176 = 174
, the Opcache managed 174
requests in a second more than without Opcache, ie it is as if 174 more people had been able to access that page in just 1 second, now imagine in an hour (of course this is all relative).
The test was done with a very simple script, if it had classes, includes, connection to bank, you can even notice a bigger improvement, follow the test script:
<?php
$nome = 'Stack OVerflow';
for ($i = 0; $i < 1000; $i++) {
echo 'Olá ', $nome, '<br>';
}
echo memory_get_peak_usage() / 1024, 'Kb';
Another example was missing:
$frase = "Bem vindo, {$nome}"
– Papa Charlie
Generally, it is not relevant. Just testing to know. And it depends on version, of the interpreter used.
– Maniero
Let me tell you something, this is micro-optimization, it doesn’t affect anything sometimes, there are things that we do in our code besides this, like create huge classes or include classes and functions without need that these yes can have performance problems for the final script. Concactenar strings will rarely make any impact, only in a very long "stress" test that will feel some variation and yet it will be minimal what is not worth the trouble. An interesting thing to use in production is Opcache or Xcache: https://answall.com/a/166747/3635
– Guilherme Nascimento