php 变量连接的单引号或双引号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3316060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Single quotes or double quotes for variable concatenation?
提问by ina
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?
哪个在处理器时间/效率方面更好?
回答by Blizz
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.htmlpoints out that it will take 4 times longer to parse those strings)
编辑:虽然有一件事 - 如果您打算在没有变量的字符串中使用一美元,请务必使用单引号!(http://www.weberdev.com/get_example-3750.html指出解析这些字符串需要 4 倍的时间)
回答by thnee
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;
回答by Flavius Stef
I generally feel that using string interpolation ("Hi, my name is $name") is better from a legibility standpoint.
"Hi, my name is $name"从易读性的角度来看,我通常认为使用字符串插值 ( ) 更好。
回答by Tres
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。除非需要双引号,否则请使用单引号。更容易打字、更容易维护、更容易解析和更容易阅读的简单约定。
回答by Martin Bean
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.
希望这是有道理的。这一切都是为了找到约定并坚持下去。
回答by Gabor de Mooij
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。而是专注于编写最易于维护的代码;正确使用注释、双引号等,即使它们可能会减慢代码速度。每一个降低代码可维护性/可读性的优化都是不好的,把它留给操作码缓存器和编译器来将你的代码转换成机器代码,不要自己做......由于优化而混淆你的源代码会回火。
回答by DkPathak
The single quoted string is better option than double quoted string while concatenating the variables. click the link for better understanding...
在连接变量时,单引号字符串比双引号字符串更好。单击链接以更好地理解...
回答by Andrejs Cainikovs
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="Hi, my name is $name",因为您不需要弄乱引号。只需使用 10 个变量对复杂的 SQL 查询进行图像化...
回答by Homer6
PHP is pretty slow:
PHP 很慢:
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 将代码缓存为字节码。您将看到该项目的速度大幅提升。
回答by stooni
$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.
我希望我有帮助。

