问题
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:
'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
}
回答2:
Never mind micro-optimization. Choose what makes your code more readable.
回答3:
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.
回答4:
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.
回答5:
There is no difference.
Learn to profile your app before asking performance related questions.
来源:https://stackoverflow.com/questions/3394166/php-sals-mall-is-emo-vs-sals-mall-is-emo-string-with-quotes-concate