PHP 中的 ' 和 " 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1402566/
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 ' and " in PHP?
提问by johnnietheblack
Possible Duplicate:
PHP: different quotes?
可能重复:
PHP:不同的引号?
Simple question:
简单的问题:
What is the difference between ' and " in php? When should I use either?
php 中的 ' 和 " 有什么区别?我应该什么时候使用?
采纳答案by Josh Davis
Basically, single-quoted strings are plain text with virtually no special case whereas double-quoted strings have variable interpolation (e.g. echo "Hello $username";) as well as escaped sequences such as "\n" (newline.)
基本上,单引号字符串是纯文本,几乎没有特殊情况,而双引号字符串具有可变插值(例如echo "Hello $username";)以及转义序列,例如“\n”(换行符)。
You can learn more about strings in PHP's manual.
您可以在PHP 手册 中了解有关字符串的更多信息。
回答by Pascal MARTIN
There are 3 syntax used to declare strings, in PHP <= 5.2 :
在 PHP <= 5.2 中,有3 种语法用于声明字符串:
With single quotes :
带单引号:
variables and escape sequences for special characters will not be expanded
特殊字符的变量和转义序列不会被扩展
For instance :
例如 :
echo 'Variables do not $expand $either';
Will output :
将输出:
Variables do not $expand $either
With double-quotes :
用双引号:
The most important feature of double-quoted strings is the fact that variable names will be expanded.
双引号字符串最重要的特性是变量名将被扩展。
For instance :
例如 :
$a = 10;
echo "a is $a";
Will output :
将输出:
a is 10
And, with heredoc :
并且,使用 heredoc :
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped,
Heredoc 文本的行为就像一个双引号字符串,没有双引号。这意味着heredoc中的引号不需要转义,
For instance :
例如 :
$a = 10;
$b = 'hello';
$str = <<<END_STR
a is $a
and "b" is $b.
END_STR;
echo $str;
Will get you :
会让你:
a is 10
and "b" is hello.
回答by David Carrington
Any variables inside a " quoted string will be parsed. Any variables in a ' quoted string will not be parsed, and will be shown literally as the variable name. For this reason, ' quoted strings are very slightly faster for PHP to process.
" 带引号的字符串中的任何变量都将被解析。 ' 带引号的字符串中的任何变量都不会被解析,并将按字面显示为变量名。因此, ' 带引号的字符串对于 PHP 的处理速度非常快。
$test = 'hello';
echo "this is a $test"; // returns this is a hello
echo 'this is a $test'; // returns this is a $test
I'd say use ' quotes unless you want variables inside your strings.
我会说使用 ' 引号,除非你想在你的字符串中使用变量。
回答by Brenton Alker
The difference is, strings between double quotes (") are parsed for variable and escape sequence substitution. Strings in single quotes (') aren't.
不同之处在于,双引号 (") 之间的字符串被解析为变量和转义序列替换。单引号 (') 中的字符串不是。
So, using double quotes (") you can do:
因此,使用双引号 (") 您可以执行以下操作:
$count = 3;
echo "The count is:\t$count";
which will produce
这将产生
The count is:<tab>3
The same in single quotes returns the literal string.
单引号中的相同返回文字字符串。
Also, the characters that need to be escaped. If you have a string like:
此外,需要转义的字符。如果你有一个像这样的字符串:
'John said, "Hello"'
you would probably use single quotes, to avoid having to escape the quotes in the string and vice-versa.
您可能会使用单引号,以避免转义字符串中的引号,反之亦然。
回答by IProblemFactory
In one word: when you would like to all your special chars (like \n) and varables (like $number) be noticed and process.
一句话:当您希望所有特殊字符(如\n)和变量(如$number)被注意到并处理时。
回答by igustin
" interprets escape characters and variables. ' doesn't do either.
" 解释转义字符和变量。 ' 也不做。

