Possible Duplicate:
PHP: different quotes?可能重复:PHP:不同的引号?
Simple question:
简单的问题:
What is the difference between ' and " in php? When should I use either?
php中'和'有什么区别?我什么时候应该使用?
6 个解决方案
#1
14
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手册中了解有关字符串的更多信息。
#2
19
There are 3 syntax used to declare strings, in PHP <= 5.2 :
在PHP <= 5.2中,有3种语法用于声明字符串:
- single quoted
- 单引号
- double quoted
- 双引号
- heredoc
- 定界符
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.
#3
3
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.
除非你想在你的字符串中使用变量,否则我会说'使用'引号。
#4
1
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.
你可能会使用单引号,以避免必须转义字符串中的引号,反之亦然。
#5
0
" interprets escape characters and variables. ' doesn't do either.
“解释转义字符和变量。”也没有。
#6
0
In one word: when you would like to all your special chars (like \n) and varables (like $number) be noticed and process.
总而言之:当你想要所有的特殊字符(如\ n)和变量(如$ number)时,请注意并处理。
#1
14
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手册中了解有关字符串的更多信息。
#2
19
There are 3 syntax used to declare strings, in PHP <= 5.2 :
在PHP <= 5.2中,有3种语法用于声明字符串:
- single quoted
- 单引号
- double quoted
- 双引号
- heredoc
- 定界符
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.
#3
3
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.
除非你想在你的字符串中使用变量,否则我会说'使用'引号。
#4
1
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.
你可能会使用单引号,以避免必须转义字符串中的引号,反之亦然。
#5
0
" interprets escape characters and variables. ' doesn't do either.
“解释转义字符和变量。”也没有。
#6
0
In one word: when you would like to all your special chars (like \n) and varables (like $number) be noticed and process.
总而言之:当你想要所有的特殊字符(如\ n)和变量(如$ number)时,请注意并处理。