PHP, The difference between single and double quotes

Well first of all, single quotes are much more efficient than double quotes, which is shown in the results from a simple test, which you can find here:http://phpbar.isgreat.org/viewtopic.php?f=2&t=56

So, there obviously is a difference between them, but what is it?

Simply put, single quotes are completely static, where as double quotes are dynamic with changing values.
For example:

CODE:  SELECT ALL
$someVar = 'more Text';
echo 'Some Text $someVar';


will output

CODE:  SELECT ALL
Some Text $someVar



However, the same example but with double quotes:

CODE:  SELECT ALL
$someVar = 'more Text';
echo "Some Text $someVar";


will output

CODE:  SELECT ALL
Some Text more Text



Obviously single quotes will have a better efficiency than double quotes, because in single quotes, php does not process anything within it, where as with double quotes, php is constantly looking through the string for variable names to check and call.

So how do we use this new knowledge to our advantage?

Well, since single quotes do not process variables how do we add variables to the string?
Well if you haven't figured it out, we simply use the string addition character.
For example:

CODE:  SELECT ALL
$str = 'more text';
echo 'some text and '.$str;


It is a bit of a hassle, but like in most things about programming, its usually efficiency vs programing time. A classic example of this is, C++ vs C#.

However, when using single quotes, depending on your php version, '\n' will output \n, so you will occasionally have to use, "\n" or PHP_EOL

well, if you still want to use double quotes, because your lazy :D
there are still times when you may find it easier to use single quotes. for example, if you are outputting static text like HTML that has mass amounts of double quotes in it. Or if you want to output the name of a variable.

Of course, you could still do

CODE:  SELECT ALL
echo "$"."varname";


instead of

CODE:  SELECT ALL
echo '$varname';


but in that case you might as well just use single quotes.

---
So yes, there is a huge difference between the single and double quotes other than simple quote escaping.

Well, i hope this helps you a lot, and from a game programming experience, ive learned that it is best to try to be as efficient as possible, ESPECIALLY when using loops. so its alright to use double quotes when your lazy, but be sure to avoid them when writing large loops, or commonly used functions.

Enjoy

 

 所以为了效率,尽量多使用 单引号吧。

 

转自http://www.devppl.com/forum/post62676.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值