变量连接用单引号还是双引号?

发布于 2024-09-11 06:03:16 字数 1432 浏览 3 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

好菇凉咱不稀罕他 2024-09-18 06:03:16

每个进行测试的人都得出结论,使用单引号的性能稍好一些。最后,单引号仅导致连接,而双引号则强制解释器解析变量的完整字符串。

然而,对于 PHP 的最新版本来说,这样做所增加的负载是如此之小,以至于大多数时候的结论是这并不重要。

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

编辑:有一件事 - 如果您要在字符串中使用单美元而不带变量,请务必使用单引号! (http://www.weberdev.com/get_example-3750.html 指出解析这些字符串将花费 4 倍的时间)

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.

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.

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)

辞取 2024-09-18 06:03:16

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

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

'\''

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

"\$"

但它也允许一些漂亮的事情,例如在末尾添加换行符:

"my string\n"

使用单引号,您必须进行串联:

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

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

然而,通常情况下人们不应该真正担心这些问题,这就是所谓的过早优化,应该避免。

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

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

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

考虑一下:

$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;

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.

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.

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.

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;
对你而言 2024-09-18 06:03:16

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

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

昔日梦未散 2024-09-18 06:03:16

就性能而言,正如其他人所证明的那样,使用单引号比使用双引号要快一些。

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

运用你的判断力。

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

比:

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

我承认:

$var = "My string.";

看起来几乎与: 一样,

$var = 'My String.';

但是后者引入的噪音更少,并且当周围有很多代码时,每一点都有帮助,更不用说使用单引号获得的其他好处了。

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

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.

孤者何惧 2024-09-18 06:03:16

从语法角度来看这并不重要。两种变体都是正确的。使用您感觉更舒服的东西。

就个人而言,我在使用 $string="Hi, my name is $name" 时感觉更好,因为您不需要弄乱引号。只需想象一下包含 10 个变量的复杂 SQL 查询...

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

囚你心 2024-09-18 06:03:16

PHP 相当慢:

http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6 -088-introduction-to-c-内存管理-和-c-面向对象编程-january-iap-2010/lecture-notes/MIT6_088IAP10_lec01.pdf

幻灯片#3

所以不要太担心关于诸如此类的小优化。

不过,请更多地关注使用 APC 将代码缓存为字节代码。您将看到该项目的速度大幅提升。

PHP is pretty slow:

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

Slide #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.

删除→记忆 2024-09-18 06:03:16

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

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

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

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

希望这是有道理的。这一切都是为了找到惯例并坚持它。

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.

橪书 2024-09-18 06:03:16

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

当你不需要包含 PHP 变量时使用单引号,否则使用双引号。
不用担心性能,只需在生产服务器上使用 APC 即可。相反,专注于编写最可维护的代码;正确使用注释、双引号等,即使它们可能会减慢代码速度。每一个降低代码可维护性/可读性的优化都是不好的,将其留给操作码缓存器和编译器将您的代码转换为机器代码,不要自己做...因为优化会引发混淆而混淆您的源代码。

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.

清晨说晚安 2024-09-18 06:03:16

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

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

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

没有心的人 2024-09-18 06:03:16
$string='Hi, my name is '.$name

从 php 和 html 结合的意义上来说,这是最好的方法!

或者像这样:

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

这是老方法!

或者像这样:

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

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

我希望我有帮助。

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

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

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!

I hope I was helpful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文