PHP单引号与双引号[重复]

时间:2022-09-15 13:36:54

This question already has an answer here:

这个问题在这里已有答案:

I am wondering the difference between the single quotes:

我想知道单引号之间的区别:

$this->db->select('card_code, card_color');

Versus the double quotes:

与双引号对比:

$this->db->select("card_code, card_color");

I know if I use double quotes I can place variables in the string, like so:

我知道如果我使用双引号我可以在字符串中放置变量,如下所示:

$this->db->select("$card_code_var, $card_color_var");

While with single quotes this does not work. And also when using it in in english or other spoken languages there can be occasions where I need to use apostrophe like here

虽然使用单引号但这不起作用。而且当用英语或其他口语语言使用时,我可能需要像这里一样使用撇号

$this->setPhrase("The cat's tail is black");

And up to here I get it.

到了这里,我明白了。

I see on the web many official guides and documentation where they use single quotes for strings. Shouldn't be single quotes used for single characters and double quotes for strings?

我在网上看到许多官方指南和文档,他们使用单引号作为字符串。不应该是单引号用于单个字符和双引号用于字符串?

$var_char = 'A';
$var_string = "A fox";

Does programmatically change something if I use a single quotes (like in the very first code above) rather than double quotes?

如果我使用单引号(如上面的第一个代码中)而不是双引号,是否以编程方式更改了某些内容?

1 个解决方案

#1


1  

One useful additional fact I may mention is speed - in case of double quotes "" PHP will search every string for variables. This really is not an issue in general day to day programming, but if you have a specific large script where performance really matters, optimizing it may pay off a bit.

我可以提到的一个有用的额外事实是速度 - 在双引号的情况下“”PHP将在每个字符串中搜索变量。这通常不是日常编程中的问题,但如果你有一个特定的大脚本,性能真的很重要,优化它可能会有所回报。

#1


1  

One useful additional fact I may mention is speed - in case of double quotes "" PHP will search every string for variables. This really is not an issue in general day to day programming, but if you have a specific large script where performance really matters, optimizing it may pay off a bit.

我可以提到的一个有用的额外事实是速度 - 在双引号的情况下“”PHP将在每个字符串中搜索变量。这通常不是日常编程中的问题,但如果你有一个特定的大脚本,性能真的很重要,优化它可能会有所回报。