PHP - “Sal's mall is $emo” vs “Sal's mall

2019-09-14 12:16发布

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.'.'

5条回答
混吃等死
2楼-- · 2019-09-14 12:47

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

查看更多
Summer. ? 凉城
3楼-- · 2019-09-14 12:50

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

查看更多
来,给爷笑一个
4楼-- · 2019-09-14 12:51
'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 
}
查看更多
萌系小妹纸
5楼-- · 2019-09-14 12:57

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.

查看更多
在下西门庆
6楼-- · 2019-09-14 13:02

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.

查看更多
登录 后发表回答