变量连接的单引号或双引号?

时间:2022-09-15 13:37:00

Is it better to concatenate a variable (say, $name) into an existing string (say, $string) like this:

是否更好地将变量(比如$ name)连接到现有字符串(比如$ string),如下所示:

$string='Hi, my name is '.$name

or to embed the variable in the string like this:

或者将变量嵌入字符串中,如下所示:

$string="Hi, my name is $name";

or is it better to use a function like this:

或者使用这样的函数更好:

$string=sprintf("Hi, my name is %s",$name);

Which is better in terms of processor time/efficiency?

哪个在处理器时间/效率方面更好?

10 个解决方案

#1


25  

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

每个进行测试的人都认为使用单引号在性能方面稍微好一些。最后,单引号只会产生连接,而双引号会强制解释器解析变量的完整字符串。

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

然而,对于PHP的最新版本而言,这样做的额外负担是如此之小,以至于大多数时候结论是它并不重要。

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

所以对于表现人来说:使用单引号。对于“我喜欢我的代码可读” - 人:双引号在易读性方面要好得多,正如Flavius Stef已经指出的那样。

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)

编辑:但有一点 - 如果你要在没有变量的字符串中使用一美元,请使用单引号! (http://www.weberdev.com/get_example-3750.html指出解析这些字符串需要4倍的时间)

#2


11  

The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

PHP中单引号和双引号之间的区别在于双引号是“智能”的,因为它们在读取时将解析变量,而单引号是“哑”并且不会尝试解析字符串中的任何字符。

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

这些导致您可以使用的字符存在一些细微差别;基本上,使用单引号时唯一需要转义的字符是单引号本身:

'\''

While if you use double quotes you have to escape other characters:

如果你使用双引号,你必须转义其他字符:

"\$"

But it also allows for some nifty things like adding a new-line to the end:

但它也允许一些漂亮的东西,比如在最后添加一个新行:

"my string\n"

With single quotes you would have to do a concatenation:

使用单引号,您必须进行连接:

'my string' . chr(10)
'my string' . "\n"

Generally, single quotes are faster because they are "dumb".

通常,单引号更快,因为它们是“哑”。

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

但是,通常人们不应该真正担心这些问题,即所谓的过早优化,应该避免。

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

关于优化的几句话:通常应该首先按照它应该的方式编写程序,然后找到最大的瓶颈并修复那些特定的瓶颈。如果字符串速度确实是PHP中的问题,您可能需要考虑切换到另一种语言。

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

关于速度:您可能希望更多地关注内存使用而不是CPU时间。在这些情况下,CPU时间可以被认为是非常恒定的。编写将迭代多次的算法时,CPU时间更相关。

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

关于连接:使用点运算符连接字符串越多,您将使用的内存越多。

Consider this:

考虑一下:

$str1 = 'asdf';
$str2 = 'qwer';

// this will result in more memory being allocated for temporary storage
echo $str1 . $str2;

// this will not allocate as much memory as the previous example
echo $str1;
echo $str2;

#3


8  

I generally feel that using string interpolation ("Hi, my name is $name") is better from a legibility standpoint.

我一般认为使用字符串插值(“嗨,我的名字是$ name”)从易读性的角度来看更好。

#4


5  

For performance, as others have proven, it is marginally faster to use single quotes rather than double quotes.

对于性能,正如其他人已经证明的那样,使用单引号而不是双引号要快一些。

Single quotes, if applied to readability science and kept away from subjectivity actually adds more "noise". Noise and how it relates to readability is talked a lot about in the book Clean Code and one could conclude that the more non-whitespace you have to see, the more it hinders readability. If applied to subjectivity, most places that I've taken the time to read actually prefer single over double quotes.

单引号,如果应用于可读性科学并远离主观性,实际上会增加更多的“噪音”。清洁代码一书中讨论了噪声及其与可读性的关系,人们可以得出结论,你必须看到的非空白越多,它就越妨碍可读性。如果应用于主观性,我花时间阅读的大多数地方实际上更喜欢单引号。

Use your judgement.

用你的判断。

$var = "My $string with $lots of $replacements."

Is much more readable than:

比以下更具可读性:

$var = 'My ' . $string . ' with ' . $lots . ' of ' . $replacements . '.';

I'll admit that:

我承认:

$var = "My string.";

Looks almost the same as:

看起来几乎相同:

$var = 'My String.';

However the latter introduces less noise and when there's lots of code around it every little bit helps, not to mention the other benefits you get from using single quotes.

然而,后者引入的噪音较少,当周围有很多代码时,每一点都有帮助,更不用说使用单引号带来的其他好处。

In the end, I prefer to KISS. Use single quotes unless you need double quotes. Simple convention that is easier to type, easier to maintain, easier to parse and easier to read.

最后,我更喜欢KISS。除非您需要双引号,否则请使用单引号。简单的约定,更容易键入,更易于维护,更易于解析和更易于阅读。

#5


0  

It doesn't matter from syntax perspective. Both variants are correct. Use what you feel more comfortable.

从语法角度来看无关紧要。两种变体都是正确的。使用你感觉更舒服的东西。

Personally, I feel better when using the $string="Hi, my name is $name", because you don't need to mess with quotes. Just image the complex SQL query with, let's say, 10 variables...

就个人而言,当我使用$ string =“嗨,我的名字是$ name”时,我感觉更好,因为你不需要乱用引号。只需使用10个变量对复杂的SQL查询进行映像...

#6


0  

PHP is pretty slow:

PHP很慢:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/lecture-notes/MIT6_088IAP10_lec01.pdf

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap- 2010 /讲课笔记/ MIT6_088IAP10_lec01.pdf

Slide #3

幻灯片#3

So don't worry too much about little optimizations like these.

因此,不要过于担心像这样的小优化。

Focus more on using APC to cache your code into byte code though. You'll see big speed gains for the project.

重点关注使用APC将代码缓存为字节代码。您会看到该项目的速度大幅提升。

#7


0  

Personally, if it's just a normal variable, or even a class property, I'd write it like this:

就个人而言,如果它只是一个普通变量,甚至是一个类属性,我会这样写:

$newVarA = "This is some text with a $variable";
$newVarB = "This is some more text, written in $settings->language";

However, if I'm using array values then I'll concatenate with single quotes.

但是,如果我使用数组值,那么我将使用单引号连接。

$newVarC = 'This is some text from a ' . $text['random'] . ' array';

Hope this makes sense. It's all about finding convention and sticking to it.

希望这是有道理的。这都是关于寻找惯例并坚持下去。

#8


0  

My motto and answer is: Leave it to the compilers to write machine code. I will tell you what I mean...

我的座右铭和答案是:将它留给编译器编写机器代码。我会告诉你我的意思......

Use single quotes when you don't need to include PHP variables, otherwise use double quotes. Dont bother about performance just use APC on production servers. Instead focus on writing the most maintainable code; use comments, double quotes etc. properly even though they may slow code down. Every optimization that decreases maintainability / readability of code is bad, leave it to the opcode-cachers and compilers to turn your code into machine code, don't do it yourself... obfuscating your source code because of optimization fires back.

如果不需要包含PHP变量,请使用单引号,否则使用双引号。不要担心性能只是在生产服务器上使用APC。而是专注于编写最易维护的代码;正确使用注释,双引号等,即使它们可能会降低代码速度。每次降低代码可维护性/可读性的优化都是不好的,请将其留给操作码 - 编译器和编译器将代码转换为机器代码,不要自己动手操作......由于优化反射而混淆源代码。

#9


0  

The single quoted string is better option than double quoted string while concatenating the variables. click the link for better understanding...

在连接变量时,单引号字符串比双引号字符串更好。点击链接以便更好地理解......

http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes

http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes

#10


-5  

$string='Hi, my name is '.$name

This is the best way, in the sense of php and html combination!

这是php和html组合意义上的最佳方式!

or like this:

或者像这样:

$string="Hi, my name is $name";

This is the old way!

这是旧的方式!

Or like this:

或者像这样:

$string=sprintf("Hi, my name is %s",$name);

This is what a programmer coming from Visual Basic or other Client Programming languages would write!

这是来自Visual Basic或其他客户端编程语言的程序员会写的!

I hope I was helpful.

我希望我能提供帮助。

#1


25  

Everyone who did the test concluded that using single quotes is marginally better performance wise. In the end single quotes result in just a concatenation while double quotes forces the interpreter to parse the complete string for variables.

每个进行测试的人都认为使用单引号在性能方面稍微好一些。最后,单引号只会产生连接,而双引号会强制解释器解析变量的完整字符串。

However the added load in doing that is so small for the last versions of PHP that most of the time the conclusion is that it doesn't really matter.

然而,对于PHP的最新版本而言,这样做的额外负担是如此之小,以至于大多数时候结论是它并不重要。

So for the performance people: use single quotes. For the "i like my code readable"-people: double quotes are a lot better for the legibility, as Flavius Stef already pointed out.

所以对于表现人来说:使用单引号。对于“我喜欢我的代码可读” - 人:双引号在易读性方面要好得多,正如Flavius Stef已经指出的那样。

Edit: One thing though - If you are going to use a a single dollar in your string without a variable, use single quotes for sure! (http://www.weberdev.com/get_example-3750.html points out that it will take 4 times longer to parse those strings)

编辑:但有一点 - 如果你要在没有变量的字符串中使用一美元,请使用单引号! (http://www.weberdev.com/get_example-3750.html指出解析这些字符串需要4倍的时间)

#2


11  

The difference between single and double quotes in PHP is that double quotes are "intelligent" in that they will parse for variables when being read, while single quotes are "dumb" and will not try to parse any character in the string.

PHP中单引号和双引号之间的区别在于双引号是“智能”的,因为它们在读取时将解析变量,而单引号是“哑”并且不会尝试解析字符串中的任何字符。

These result in some minor differences in what characters you can use; basically, the only character you need to escape when using single quotes is a single quote itself:

这些导致您可以使用的字符存在一些细微差别;基本上,使用单引号时唯一需要转义的字符是单引号本身:

'\''

While if you use double quotes you have to escape other characters:

如果你使用双引号,你必须转义其他字符:

"\$"

But it also allows for some nifty things like adding a new-line to the end:

但它也允许一些漂亮的东西,比如在最后添加一个新行:

"my string\n"

With single quotes you would have to do a concatenation:

使用单引号,您必须进行连接:

'my string' . chr(10)
'my string' . "\n"

Generally, single quotes are faster because they are "dumb".

通常,单引号更快,因为它们是“哑”。

However, normally one should not really worry about these issues, that is called Premature optimization, and should be avoided.

但是,通常人们不应该真正担心这些问题,即所谓的过早优化,应该避免。

A couple of words about optimization: generally one should first write the program the way it should work, and then find the biggest bottlenecks and fix those particular ones. If string speed really is an issue for you in PHP, you might want to consider switching to another language.

关于优化的几句话:通常应该首先按照它应该的方式编写程序,然后找到最大的瓶颈并修复那些特定的瓶颈。如果字符串速度确实是PHP中的问题,您可能需要考虑切换到另一种语言。

Regarding speed: you probably want to focus more on memory usage than on CPU time. In these cases the CPU time could be considered pretty constant. CPU time is more relevant when writing algorithms that will iterate many times.

关于速度:您可能希望更多地关注内存使用而不是CPU时间。在这些情况下,CPU时间可以被认为是非常恒定的。编写将迭代多次的算法时,CPU时间更相关。

Regarding concatenations: the more you concatenate strings using the dot-operator, the more memory you will be using.

关于连接:使用点运算符连接字符串越多,您将使用的内存越多。

Consider this:

考虑一下:

$str1 = 'asdf';
$str2 = 'qwer';

// this will result in more memory being allocated for temporary storage
echo $str1 . $str2;

// this will not allocate as much memory as the previous example
echo $str1;
echo $str2;

#3


8  

I generally feel that using string interpolation ("Hi, my name is $name") is better from a legibility standpoint.

我一般认为使用字符串插值(“嗨,我的名字是$ name”)从易读性的角度来看更好。

#4


5  

For performance, as others have proven, it is marginally faster to use single quotes rather than double quotes.

对于性能,正如其他人已经证明的那样,使用单引号而不是双引号要快一些。

Single quotes, if applied to readability science and kept away from subjectivity actually adds more "noise". Noise and how it relates to readability is talked a lot about in the book Clean Code and one could conclude that the more non-whitespace you have to see, the more it hinders readability. If applied to subjectivity, most places that I've taken the time to read actually prefer single over double quotes.

单引号,如果应用于可读性科学并远离主观性,实际上会增加更多的“噪音”。清洁代码一书中讨论了噪声及其与可读性的关系,人们可以得出结论,你必须看到的非空白越多,它就越妨碍可读性。如果应用于主观性,我花时间阅读的大多数地方实际上更喜欢单引号。

Use your judgement.

用你的判断。

$var = "My $string with $lots of $replacements."

Is much more readable than:

比以下更具可读性:

$var = 'My ' . $string . ' with ' . $lots . ' of ' . $replacements . '.';

I'll admit that:

我承认:

$var = "My string.";

Looks almost the same as:

看起来几乎相同:

$var = 'My String.';

However the latter introduces less noise and when there's lots of code around it every little bit helps, not to mention the other benefits you get from using single quotes.

然而,后者引入的噪音较少,当周围有很多代码时,每一点都有帮助,更不用说使用单引号带来的其他好处。

In the end, I prefer to KISS. Use single quotes unless you need double quotes. Simple convention that is easier to type, easier to maintain, easier to parse and easier to read.

最后,我更喜欢KISS。除非您需要双引号,否则请使用单引号。简单的约定,更容易键入,更易于维护,更易于解析和更易于阅读。

#5


0  

It doesn't matter from syntax perspective. Both variants are correct. Use what you feel more comfortable.

从语法角度来看无关紧要。两种变体都是正确的。使用你感觉更舒服的东西。

Personally, I feel better when using the $string="Hi, my name is $name", because you don't need to mess with quotes. Just image the complex SQL query with, let's say, 10 variables...

就个人而言,当我使用$ string =“嗨,我的名字是$ name”时,我感觉更好,因为你不需要乱用引号。只需使用10个变量对复杂的SQL查询进行映像...

#6


0  

PHP is pretty slow:

PHP很慢:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap-2010/lecture-notes/MIT6_088IAP10_lec01.pdf

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-088-introduction-to-c-memory-management-and-c-object-oriented-programming-january-iap- 2010 /讲课笔记/ MIT6_088IAP10_lec01.pdf

Slide #3

幻灯片#3

So don't worry too much about little optimizations like these.

因此,不要过于担心像这样的小优化。

Focus more on using APC to cache your code into byte code though. You'll see big speed gains for the project.

重点关注使用APC将代码缓存为字节代码。您会看到该项目的速度大幅提升。

#7


0  

Personally, if it's just a normal variable, or even a class property, I'd write it like this:

就个人而言,如果它只是一个普通变量,甚至是一个类属性,我会这样写:

$newVarA = "This is some text with a $variable";
$newVarB = "This is some more text, written in $settings->language";

However, if I'm using array values then I'll concatenate with single quotes.

但是,如果我使用数组值,那么我将使用单引号连接。

$newVarC = 'This is some text from a ' . $text['random'] . ' array';

Hope this makes sense. It's all about finding convention and sticking to it.

希望这是有道理的。这都是关于寻找惯例并坚持下去。

#8


0  

My motto and answer is: Leave it to the compilers to write machine code. I will tell you what I mean...

我的座右铭和答案是:将它留给编译器编写机器代码。我会告诉你我的意思......

Use single quotes when you don't need to include PHP variables, otherwise use double quotes. Dont bother about performance just use APC on production servers. Instead focus on writing the most maintainable code; use comments, double quotes etc. properly even though they may slow code down. Every optimization that decreases maintainability / readability of code is bad, leave it to the opcode-cachers and compilers to turn your code into machine code, don't do it yourself... obfuscating your source code because of optimization fires back.

如果不需要包含PHP变量,请使用单引号,否则使用双引号。不要担心性能只是在生产服务器上使用APC。而是专注于编写最易维护的代码;正确使用注释,双引号等,即使它们可能会降低代码速度。每次降低代码可维护性/可读性的优化都是不好的,请将其留给操作码 - 编译器和编译器将代码转换为机器代码,不要自己动手操作......由于优化反射而混淆源代码。

#9


0  

The single quoted string is better option than double quoted string while concatenating the variables. click the link for better understanding...

在连接变量时,单引号字符串比双引号字符串更好。点击链接以便更好地理解......

http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes

http://www.codeforest.net/php-myth-busters-using-single-quotes-on-string-is-faster-then-double-quotes

#10


-5  

$string='Hi, my name is '.$name

This is the best way, in the sense of php and html combination!

这是php和html组合意义上的最佳方式!

or like this:

或者像这样:

$string="Hi, my name is $name";

This is the old way!

这是旧的方式!

Or like this:

或者像这样:

$string=sprintf("Hi, my name is %s",$name);

This is what a programmer coming from Visual Basic or other Client Programming languages would write!

这是来自Visual Basic或其他客户端编程语言的程序员会写的!

I hope I was helpful.

我希望我能提供帮助。