PHP - “Sal's mall is $emo” vs “Sal's mall is ”.$emo - string with quotes concatenation efficiency [closed]

核能气质少年 提交于 2019-12-11 16:40:47

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!