views:

2710

answers:

9

Is there a difference or is one better than the other of the following:

$var = '';
...
$var .= 'blah blah';
$var .= $var2;
$var .= 'blah blah';

Or

$var = '';
...
$var .= 'blah blah' . $var2 . 'blah blah';

Is there a speed difference or is there a reason why you'd pick one over the other?

A: 

I don't think you'll notice for such a short situation. I typically decide for readability reasons which I'm going to use. I only split on two situations, otherwise on one line:

a) Creating a multi-line response so the code somewhat mimics what the user will see. I've found it makes it easier to catch typos and mistakes this way.

$var  = "line 1 \n";
$var .= "line 2 \n";

b) line length. If the line will overflow my window I'll typically break it up so I don't have to go to the end of the line to read the end. Most languages support a line continuation character so I don't really need to break it physically in the code, but just a habit i have since I do work in some languages that don't have it.

To "break it physically in the code", do you punch your computer or just us a really small magnet?
thrashr888
+6  A: 

There is a small difference if you echo the string.

echo $var1,$var2,$var3 //comma (echo with multiple arguments)

is faster then

echo $var1.$var2.$var3 // dot (concat and echo)
Ivan
that wasn't the question though...
nickf
+6  A: 

It doesn't matter for a few, short, strings. Use whatever is clearer to read.

But if you have lots of them (say thousands) not so short ones, you'll probably want to keep them in an array and join them together. (As it happens such code is often as easy to read as concatenating the strings using ".=", so you're not sacrificing clarity on the altar of performance.)

EDIT: I don't think my above thinking is valid. PHP's strings are mutable, right?

PEZ
sorry to be undeleting this PEZ, but it seems like a good answer which others thought helpful so I took the liberty.
nickf
I'm worried that people will think that it's actually true. Which is probably isn't for PHP. It was a spinal response from me since most languages I work with have immutable strings.
PEZ
+2  A: 

For anything you're concatenating manually in code, performance probably doesn't matter, so readability is king. For me, this often means using heredoc syntax. I don't like the way it breaks my indentation structure, but it's especially nice when you want line breaks and/or tabs inserted correctly into your strings.

grossvogel
A: 

The fastest way to get a large amount of data into a single string, is output buffering to capture echo'd output.

ob_start();
/* many echos. For example, in a loop */
$outString = ob_get_contents(); // get the complete string
ob_end_clean();

In general, it's unlikely to make a significant difference to the speed of your program, so make it look obvious what you are doing, since you have to come back to fix/update it later anyhow. Readability is king.

Alister Bulman
How can you say that output buffering is better than multiple variable assignment? Did you have any text explaining the inner working of php interpreter to point me to?
Eineki
It's about the OB code allocating larger memory spaces to put data into. Concatenating strings also requires memory allocations - for the new string - and this takes time.
Alister Bulman
+4  A: 

Both PEZ and Topbit are correct. I just want to point out something I think looks better than has been seen here:

$var  = "line 1 \n";
$var .= "line 2 \n";

versus

$var = "line 1 \n"
     . "line 2 \n";

I much prefer the second one to the first one as visually it is obvious your result is the one string $var. It also prevents stupid bugs like:

$var  = "line 1 \n";
$var .= "line 2 \n";
$vat .= "line 3 \n";
$var .= "line 4 \n";

EDIT: Yes, the semi-colon was a typo.

jmucchiello
+1, I can't count the times I've had bugs like that. Is that semicolon a typo or does it really work starting with a dot like that after a semicolon?
PEZ
The semicolon's a typo
Ciaran McNulty
didn't see the error at first ... i guess that proves your point!
Cassy
I prefer the first one, because it's compatible with auto-indent function in both eclipse and vim
Bob Fanger
+2  A: 

Won't make a difference. Both are concatenation, so whatever is easiest to read is what you should use.

If you're after best-performing, try using an array and then implode it when you've finished.

$var = array();
$var[] = "line 1\n";
$var[] = "line 2\n";
$var[] = "line 3";
$var = implode('', $var);

Any way you do it the speed difference will be negligible unless you are working with thousands of big strings.

Christopher Nadeau
A: 

There is no meaningful difference. It's just a matter of coding preference.

Hans