Why? Because in the first two examples PHP is having to allocate memory for multiple strings and then go through the (relatively) expensive process of “sticking” them together again. If you just declare a string over multiple lines you are avoiding that overhead. You have to be careful to make code readable if you do this but I’ve never had any problems with it.
Does this mean that multi-line echo statements are are better written as:
Not had that problem, but none of the editors I use are listed there.
I think it’s all about situation. For SQL white space is used to format the code, so multi line is the way to go. For HTML, I echo as I would have typed the html, ie each line is echoed separately.
Seems to be fixed, at least in NotePad++ (though it doesn’t highlight SQL keywords or HTML keywords in a string - though how would it know that the string is meant to be sql).
I have one realistic reason why embedded newlines may be an issue - testing. The resulting string differs depending on how far it is indented, and on whether spaces or tabs are used to indent it.
That has been the cause of some issues when it comes to unit testing.
Hi spivurno.
Just remember one simple rule: not a single syntax issue have any performance importance. You can use any syntax you wish and never worry about performance.
So, I am totally agree with hash - it won’t make any significant difference.
Also bear in mind that these coding tips aren’t treasure of wisdom.
Some of them obsoleted and some of them should be titled “Coding standards” and separated from the tips and some of them just not a truth.
That topic badly need to be edited and sorted out.
Hi zalucius.
Do you really understand the technology? Define “transmission” then. What do you mean?
What if we also have HTTP server between PHP and client?
Im not too deep into how the exact inner workings of PHP and output to client works. Thus “transmission” might have been a bad choice of word…
In my book, every echo/print statement results in some data being sent somewhere.
Keeping the data on the PHP side, until all output is ready, will result in fewer connections/streams/transmissions,… or what its called…
I tried to do a little digging on the subject, but didnt really find anything usefull… perhaps you have some useful links/topics I could have a look at?
Honestly, if your worrying about whether calling echo or print once or multiple times is going to affect efficiency you need to step back and get your priorities in order. For me it comes down what is more readable. If using several calls to echo is more readable then a single call then so be it. It just depends on the situation there isn’t really any absolute. The same can be said for printf, sprintf vs. echo and string concatenation. It more or less comes down to personal preference considering the code is going to conduct itself in pretty much the same way. Micromanaging these things are hardly worth the time of day.