views:

76

answers:

5

In PHP, which is a better way to concatenate strings (with a single-quote) in terms of resources?

"Sal's mall is $emo."

or

"Sal's mall is ".$emo.'.'

or

'Sal\'s mall is '.$emo.'.'

+1  A: 
'Sal\'s mall is '.$emo.'.'

3rd way is more efficient (slightly). You can test it by yourself doing a loop:

for ($i = 0; $i < 100000; $i++)
{ // enter code here 
}
Bald
It's useless test. It doesn't test anything real. You cannot find any difference in the real application. Learn to use profiling instead of such ridiculous loops
Col. Shrapnel
@Col. Shrapnel, That's not what the question is asking at all.
strager
@strager too bad for the question
Col. Shrapnel
+1  A: 

Well, when you use single quotes, PHP assumes it's just a string, but if you use double quotes, it's gonna parse it to find variables inside. So, using single quotes and concatenation is more efficient. Anyways, you have to test it for yourself and compare the results.

Hamid Nazari
PHP is going to parse single quoted strings anyway. At least to find a closing quote.
Col. Shrapnel
And it's going to have to parse the `.`'s and the `$emo` anyway, too.
strager
+1  A: 

Trust me... if you have to ask, there's not going to be any meaningful difference in speed relative to the rest of your page load.

philfreo
+3  A: 

Never mind micro-optimization. Choose what makes your code more readable.

Anax
+1 for truth, I have never run into a situation where strings are the bottleneck.
Kristoffer S Hansen
A: 

There is no difference.
Learn to profile your app before asking performance related questions.

Col. Shrapnel
There is a very slight difference, but it's not worth caring about. The gains in maintainability far outweigh the couple of CPU cycles' difference.
cHao
@cHao no, there is not. and there is no gains in maintainability as well
Col. Shrapnel
@Col. Shrapnel: There is. It's not worth the trouble of measuring, but it's there. You're forming the string in two rather different ways: either all at once (with the double-quoted string), or by catting 3 strings together. So there's that to consider. Then there's the fact that in a single-quoted string, PHP doesn't interpolate (which changes how long it takes to init the string).
cHao
Add to that, `"There are $count lights"` is easier to read, and harder to screw up the quotes and spaces and such on (read: easier to maintain), than `'There are '.$count.' lights'`..
cHao
@cHao `PHP doesn't interpolate` - so what? it's still being darn interpolated before concatenation. same action, different place. you cannot save PHP from interpolation a variable by this. you have to interpolate a variable and init resulting string ANYWAY. can't you see it?
Col. Shrapnel
Outside a string, the variable isn't interpolated -- it's just retrieved. `"I say $x"` needs to be searched for variables to replace, and there's a bit more to that than just looking for dollar signs. `'I say '.$x` doesn't, but there's the cost of concatenation. The string's being constructed two different ways, so they should be expected to have different performance characteristics. But again, the performance is similar enough that it's rarely worth caring which is faster -- readability trumps micro-optimization.
cHao
@cHao where does it "search for variables" and why it doesn't "search" outside of the string? what is the difference between "interpolation" and "just retrieving"? Rein your fantasy a bit, eh?
Col. Shrapnel
Interpolation is replacing the variable *in the string* with its value. IE: replacing "$foo" with the value of $foo. That's not necessary if the string is single-quoted -- and appending the variable's value to a string is a different operation than interpolating it in the string. The fact that you can't or won't see that doesn't make it any less true.
cHao
See http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5 , and note the times in the accepted answer. Course, seeing that question and answer, i have to vote to close now.
cHao
@cHao lol. you still fail to see that for the RESULTING string we still have all these operations. and your magic "interpolation" is no more than just concatenation. of string contents and a variable value.
Col. Shrapnel
Interpolation and concatenation are two different things. You can't do "There are $x lights" using just the string 'There are $x lights' and concatenation. You need to either replace '$x' with the value of $x, or decide at runtime how to break the string up into pieces that can be concatenated. And that decision takes time.
cHao
oh yes. that decision takes time. enormous difference
Col. Shrapnel
I didn't say it was an enormous difference. In fact, i've said almost the opposite -- that any difference is too small to worry about, in the grand scheme of things -- the whole time. But there's a difference between "There is no difference worth losing sleep over" and "There is no difference". The latter ignores the reality of things.
cHao