PHP中的单引号和双引号的字符串有什么区别?
我有点困惑,为什么我会在PHP中看到一些代码,其中有单个引号,有时用双引号放置。
我只知道在.NET或C语言中,如果是单语言,则意味着它是字符,而不是字符串。
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
两种封闭的字符都是字符串。一种类型的报价可方便地用来包装另一种类型的报价。
“'”
和'“''
。引号类型之间的最大区别是,封闭式标识符引用被用代替内部双引号,而不是在单个引号中。Both kinds of enclosed characters are strings. One type of quote is conveniently used to enclose the other type of quote.
"'"
and'"'
. The biggest difference between the types of quotes is that enclosed identifier references are substituted for inside double quotes, but not inside single quotes.有些人可能会说我有点偏离主题,但是无论如何:
您不一定要选择字符串的内容:
echo“是\”游戏\“时间。”;
或echo'it \'s“ game” time。';
如果您熟悉使用英文引号和撇号的正确字符,您可以使用双引号或单个引号,因为它不再重要:
echo“是“游戏”时间。”;
和echo'这是“游戏”时间。';
当然,您还可以在需要时添加变量。只是不要忘记,只有在双引号中才能评估它们!
Some might say that I'm a little off-topic, but here it is anyway:
You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time.";
orecho 'It\'s "game" time.';
If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time.";
andecho 'It’s “game” time.';
Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!
php strings 不仅可以指定两种方式,但以四种方式。
\'
逃脱它,并且要显示后斜线,您可以使用另一个后斜线\\ (是的,即使是单个引用的字符串也被解析)。
$ type
,并且要echo“ $ type是”
。这将寻找变量$ types
。要解决此使用echo“ {$ type} s是”
。看看查看如何使用数组变量等。<<<
开始。在此操作员之后,提供了标识符,然后提供了一个newline。字符串本身遵循,然后再次使用相同的标识符来关闭报价。您无需在本语法中逃脱引号。<<<
序列用于Heredocs,但是随后的标识符以单引号包含,例如&lt;&lt;&lt;'eot'< /代码>。 在Nowdoc中未完成解析。
注意:
必须逃脱单语引号内的单引号和双引号的双引号:
速度:
没有差异。
请阅读a
引用的字符串仅被解析曾经以及整个脚本,然后将其翻译成OpCode。然后将执行一百万次。因此,除了解析外,它都可以衡量任何东西。那只是冰山一角。对于这样的纳米计算,几乎不可能创建一个可靠的测试,而这些测试不会因某些干扰的副作用而破坏。
PHP strings can be specified not just in two ways, but in four ways.
\'
, and to display a back slash, you can escape it with another backslash\\
(So yes, even single quoted strings are parsed).$type
and you want toecho "The $types are"
. That will look for the variable$types
. To get around this useecho "The {$type}s are"
. Take a look at string parsing to see how to use array variables and such.<<<
. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. You don't need to escape quotes in this syntax.<<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g.<<<'EOT'
. No parsing is done in nowdoc.Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this
The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.
事情以双引号进行评估,但不会单一评估:
Things get evaluated in double quotes but not in single:
'
单人引用指定字符串的最简单方法是将其包装在单引号中。单引号中的所有内容都被视为平弦。
示例:
“
双引号中的双引号插值变量和广泛的逃生序列。注意:使用Curly Braces
{}
包括复杂 下一个字符是变量名称的有效。
字符
变量或在变量
之后的 一次解析整个代码,然后将结果字节码存储在。
'
Single quotedThe simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.
Example:
"
Double quotedStrings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces
{}
to include complex variables or in case when next character after a variable is a valid character for a variable name.Example:
Is there a performance benefit single quote vs double quote in PHP?
No.
In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.
单引号的字符串没有解释其中的变量。双引号的字符串确实可以。
另外,双引号的字符串可以包含无反冲的撇号,而单引号的字符串可以包含未示例的引号。
A single-quoted string does not have variables within it interpreted. A double-quoted string does.
Also, a double-quoted string can contain apostrophes without backslashes, while a single-quoted string can contain unescaped quotation marks.
在PHP中,
'我的名字'
和“我的名字”
是字符串。您可以在php手册上阅读更多有关它的信息。您应该知道的是
PHP中,人们使用单个报价来定义一个常数字符串,例如
'a''
,'我的名字'
,'abc xyz'< /code>,在使用双引号定义字符串时包含标识符时,例如
“ a $ b $ c $ d”
。In PHP, both
'my name'
and"my name"
are string. You can read more about it at the PHP manual.Thing you should know are
In PHP, people use single quote to define a constant string, like
'a'
,'my name'
,'abc xyz'
, while using double quote to define a string contain identifier like"a $b $c $d"
.