1

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.'.'
Rizier123
  • 58,877
  • 16
  • 101
  • 156
ina
  • 19,167
  • 39
  • 122
  • 201
  • 1
    Depends on the situation, generally if you can i'd use the first method, falling back to the second if you have to, and finally the third. I don't think theres a huge efficiency benefit to be had here, it's more of a maintainability thing. – studioromeo Aug 03 '10 at 06:51
  • If you have the luxury of optimizing string parsing, your application is already running very quickly, so you should feel very confident. String parsing speed will never be your bottle neck. – nikc.org Aug 03 '10 at 07:11
  • That's why PHP is despised by everyone - because of all these answers below. – Your Common Sense Aug 03 '10 at 07:19
  • That's why PHP *programmers* are despised. :P I don't mind PHP; I mind the people who write horribly contorted code to squeeze an extra CPU cycle out of string parsing instead of fixing their broke-ass algorithms. – cHao Aug 04 '10 at 13:35
  • there's nothing wrong with trying to write efficient *lean* code. don't waste resources when it's not needed. it all adds up – ina Aug 04 '10 at 13:45
  • there is no resources to waste. and, to tell you truth. show me your application and I point you things that eat 1000000 times more resources than these poor quotes you are so concerned of. – Your Common Sense Aug 04 '10 at 14:12
  • @ina: There's nothing wrong with trying to write efficient code, til you get into that whole tunnel-vision mode where you're trying to create a string as fast as possible, instead of looking at the bigger picture and seeing that you're probably using 1000000x as much CPU power somewhere else. The .0001% you might save by using the "best" method is nothing compared to that ugly algorithm that's eating up millions of times as much processing power as it needs to. And that's not even mentioning the extra time it'd take *you* to mentally parse and maintain a format you're not familiar with. – cHao Aug 04 '10 at 15:41
  • ok, the thing is, i'm actually familiar with a harder-to-read format that might be slightly more efficient. but that's because i've been coding in a closet for a long time (don't ask). now, i'm looking at other people's code, and i notice they format their strings very differently. so i thought which one's better, might as well go to that. surprised to find that the harder-to-read way that i'm used to actually is more efficient (even by a miniscule bit) – ina Aug 04 '10 at 16:07
  • in either case, if code is in that single quote harder-to-read way, you can just write a "de-obsfucator" to convert it to doublequotes – ina Aug 04 '10 at 16:08
  • no, it's just matter of taste(or agreement). google for `coding standards` to get an example of such agreements. and just remember as a rule of thumb: no syntax issue can really affect performance. only data manipulation can. and learn profiling – Your Common Sense Aug 04 '10 at 16:18
  • possible duplicate of [Speed difference in using inline strings vs concatenation in php5?](http://stackoverflow.com/questions/13620/speed-difference-in-using-inline-strings-vs-concatenation-in-php5) – cHao Aug 05 '10 at 02:19

5 Answers5

5

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

Anax
  • 9,122
  • 5
  • 34
  • 68
1
'Sal\'s mall is '.$emo.'.'

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

for ($i = 0; $i < 100000; $i++) {
    // enter code here 
}
lorem monkey
  • 3,942
  • 3
  • 35
  • 49
Bald
  • 2,156
  • 3
  • 24
  • 33
1

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
  • 3,905
  • 2
  • 28
  • 31
1

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
  • 41,941
  • 26
  • 128
  • 141
-2

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

cHao
  • 84,970
  • 20
  • 145
  • 172
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 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 Aug 04 '10 at 13:39
  • @cHao no, there is not. and there is no gains in maintainability as well – Your Common Sense Aug 04 '10 at 14:10
  • @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 Aug 04 '10 at 14:40
  • 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 Aug 04 '10 at 14:42
  • @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? – Your Common Sense Aug 04 '10 at 15:10
  • 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 Aug 04 '10 at 15:28
  • @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? – Your Common Sense Aug 04 '10 at 15:35
  • 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 Aug 05 '10 at 02:10
  • 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 Aug 05 '10 at 02:18
  • @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. – Your Common Sense Aug 05 '10 at 04:16
  • 2
    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 Aug 05 '10 at 08:16
  • oh yes. that decision takes time. enormous difference – Your Common Sense Aug 07 '10 at 04:20
  • 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 Oct 03 '10 at 18:03