PHP 中的单引号和双引号字符串有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3446216/
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
What is the difference between single-quoted and double-quoted strings in PHP?
提问by rob waminal
I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
我有点困惑为什么我在 PHP 中看到一些代码,字符串放在单引号中,有时放在双引号中。
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.
我只知道在 .NET 或 C 语言中,如果它在单引号中,则表示它是一个字符,而不是一个字符串。
回答by Peter Ajtai
PHP stringscan be specified not just in twoways, but in fourways.
PHP 字符串不仅可以通过两种方式指定,还可以通过四种方式指定。
- Single quoted stringswill display things almost completely "as is." Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash
\'
, and to display a back slash, you can escape it with another backslash\\
(So yes, even single quoted strings are parsed). - Double quote stringswill display a host of escaped characters (including some regexes), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let's say you have the variable
$type
and you want toecho "The $types are"
. That will look for the variable$types
. To get around this useecho "The {$type}s are"
You can put the left brace before or after the dollar sign. Take a look at string parsingto see how to use array variables and such. - Heredocstring syntax works like double quoted strings. It starts with
<<<
. 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. - Nowdoc(since PHP 5.3.0) string syntax works essentially like single quoted strings. The difference is that not even single quotes or backslashes have to be escaped. A nowdoc is identified with the same
<<<
sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g.<<<'EOT'
. No parsing is done in nowdoc.
- 单引号字符串将几乎完全“按原样”显示内容。不会解释变量和大多数转义序列。例外是要显示文字单引号,您可以使用反斜杠对其进行转义
\'
,而要显示反斜杠,您可以使用另一个反斜杠对其进行转义\\
(因此,是的,即使是单引号字符串也会被解析)。 - 双引号字符串将显示大量转义字符(包括一些正则表达式),并且将评估字符串中的变量。这里的一个重点是您可以使用花括号来隔离您要评估的变量的名称。例如,假设您有变量
$type
并且想要echo "The $types are"
. 那将寻找变量$types
。为了避免这种用法,echo "The {$type}s are"
您可以在美元符号之前或之后放置左大括号。查看字符串解析以了解如何使用数组变量等。 - Heredoc字符串语法的工作原理类似于双引号字符串。它以
<<<
. 在这个运算符之后,提供了一个标识符,然后是一个换行符。紧随其后的是字符串本身,然后再次使用相同的标识符来关闭引号。您不需要在此语法中转义引号。 - Nowdoc(自 PHP 5.3.0 起)字符串语法本质上类似于单引号字符串。不同之处在于,甚至不必转义单引号或反斜杠。nowdoc 用与heredocs 相同的
<<<
序列标识,但后面的标识符用单引号括起来,例如<<<'EOT'
。nowdoc 中没有进行解析。
Notes:Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:
注意:单引号内的单引号和双引号内的双引号必须转义:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
Speed:
I would not put too much weight on single quotes being faster than double quotes. They probably are faster in certain situations. Here's an article explaining one manner in which single and double quotes are essentially equally fast since PHP 4.3(Useless Optimizations
toward the bottom, section C
). Also, this benchmarks pagehas a single vs double quote comparison. Most of the comparisons are the same. There is one comparison where double quotes are slower than single quotes.
速度:
我不会太看重单引号比双引号更快。在某些情况下,它们可能更快。这是一篇文章,解释了自 PHP 4.3 起单引号和双引号基本上同样快的一种方式(Useless Optimizations
底部,部分C
)。此外,这个基准测试页面有单引号和双引号的比较。大多数比较是相同的。有一种比较,双引号比单引号慢。
回答by Dani
Things get evaluated in double quotes but not in single:
事情用双引号而不是单引号来评估:
$s = "dollars";
echo 'This costs a lot of $s.'; // This costs a lot of $s.
echo "This costs a lot of $s."; // This costs a lot of dollars.
回答by Janak Kanani
'
Single quoted
'
单引号
The simplest way to specify a string is to enclose it in single quotes. Single quote is generally faster, and everything quoted inside treated as plain string.
指定字符串的最简单方法是将其括在单引号中。单引号通常更快,并且里面引用的所有内容都被视为纯字符串。
Example:
例子:
echo 'Start with a simple string';
echo 'String\'s apostrophe';
echo 'String with a php variable'.$name;
"
Double quoted
"
双引号
Use double quotes in PHP to avoid having to use the period to separate code (Note: Use curly braces {}
to include variables if you do not want to use concatenation (.
) operator) in string.
在 PHP 中使用双引号可以避免在字符串中使用句点分隔代码(注意:{}
如果不想使用连接 ( .
) 运算符,请使用大括号包含变量)。
Example:
例子:
echo "Start with a simple string";
echo "String's apostrophe";
echo "String with a php variable {$name}";
Is there a performance benefit single quote vs double quote in PHP?
PHP 中单引号和双引号有性能优势吗?
Yes. It is slightly faster to use single quotes.
是的。使用单引号稍微快一些。
PHP won't use additional processing to interpret what is inside the single quote. when you use double quotes PHP has to parse to check if there are any variables within the string.
PHP 不会使用额外的处理来解释单引号内的内容。当您使用双引号时,PHP 必须解析以检查字符串中是否有任何变量。
回答by Borealid
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.
此外,双引号字符串可以包含不带反斜杠的撇号,而单引号字符串可以包含未转义的引号。
The single-quoted strings are faster at runtime because they do not need to be parsed.
单引号字符串在运行时速度更快,因为它们不需要被解析。
回答by Bang Dao
In PHP, both 'my name'
and "my name"
are string. You can read more about it at the PHP manual.
在 PHP 中,'my name'
和"my name"
都是字符串。您可以在 PHP 手册中阅读有关它的更多信息。
Thing you should know are
你应该知道的是
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
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"
.
在 PHP 中,人们使用单引号来定义常量字符串,例如'a'
, 'my name'
, 'abc xyz'
,而使用双引号来定义包含标识符的字符串,例如"a $b $c $d"
.
And other thing is,
而另一件事是,
echo 'my name';
is faster than
比
echo "my name";
but
但
echo 'my ' . $a;
is slower than
比
echo "my $a";
This is true for other used of string.
对于字符串的其他用法也是如此。
回答by Rabindra Nath
Example of single, double, heredoc, and nowdoc quotes
单引号、双引号、heredoc 和 nowdoc 引号的示例
<?php
$fname = "David";
// Single quotes
echo 'My name is $fname.'; // My name is $fname.
// Double quotes
echo "My name is $fname."; // My name is David.
// Curly braces to isolate the name of the variable
echo "My name is {$fname}."; // My name is David.
// Example of heredoc
echo $foo = <<<abc
My name is {$fname}
abc;
// Example of nowdoc
echo <<< 'abc'
My name is "$name".
Now, I am printing some
abc;
?>
回答by Maulik Bhojani
In PHP, single quote text is considered as string value and double quote text will parse the variables by replacing and processing their value.
在 PHP 中,单引号文本被视为字符串值,双引号文本将通过替换和处理它们的值来解析变量。
$test = "variable";
echo "Hello Mr $test"; // the output would be: Hello Mr variable
echo 'Hello Mr $test'; // the output would be: Hello Mr $test
Here, double quote parse the value and single quote is considered as string value (without parsing the $test
variable.)
这里,双引号解析值,单引号被视为字符串值(不解析$test
变量。)
回答by wallyk
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.
两种封闭的字符都是字符串。一种类型的引用可以方便地用于包含另一种类型的引用。 "'"
和'"'
。引号类型之间的最大区别是括起来的标识符引用被替换为双引号内,而不是单引号内。
回答by Hamza
The difference between using single quotes and double quotes in php is that if we use single quotes in echo statement then it is treated as a string. ... but if we enter variable name within double quotes then it will output the value of that variable along with the string.
在php中使用单引号和双引号的区别在于,如果我们在echo语句中使用单引号,那么它被视为一个字符串。...但是如果我们在双引号内输入变量名称,那么它将输出该变量的值以及字符串。
回答by teenage vampire
Here some possibilities of single and double quotes with variable
这里有一些带变量的单引号和双引号的可能性
$world = "world";
"Hello '.$world.' ";
'hello ".$world."';