1. 云栖社区>
  2. PHP教程>
  3. 正文

PHP, The difference between single and double quotes

作者:用户 来源:互联网 时间:2017-12-01 16:04:08

phpdoublesingleBETWEEN

PHP, The difference between single and double quotes - 摘要: 本文讲的是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 simpl

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=56So, 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_EOLwell, if you still want to use double quotes, because your lazy PHP, The difference between single and double quotesthere 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

以上是云栖社区小编为您精心准备的的内容,在云栖社区的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索php , double , single , BETWEEN , ,以便于您获取更多的相关知识。