I'm not an expert in PHP programming, but I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.
我不是PHP编程方面的专家,但我有点搞不懂为什么我在PHP中看到一些代码,这些代码都是单引号,有时是双引号。
I just know in .NET, or the C language, if it is in single quote, that means it is a character, not a string.
我只知道。net,或者C语言,如果是单引号,那就意味着它是一个字符,而不是字符串。
11 个解决方案
#1
905
PHP strings can be specified not just in two ways, but in four ways.
PHP字符串不仅可以通过两种方式指定,还可以通过四种方式来指定。
-
Single quoted strings will 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 strings will 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 what 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 parsing to see how to use array variables and such. - 双引号字符串将显示一组转义字符(包括一些regex),字符串中的变量将被计算。这里的一个重点是,您可以使用花括号来隔离要评估的变量的名称。例如,假设您有一个变量$type,并且您要响应“$types”,它将查找变量$类型。要绕过这个使用echo,“{$type}”是“您可以将左括号放在美元符号之前或之后”。看看字符串解析,看看如何使用数组变量。
-
Heredoc string 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. - Nowdoc(因为PHP 5.3.0)的字符串语法本质上类似于单引号字符串。不同之处在于,即使单引号或反斜杠也不能被转义。现在,一个nowdoc被标识为与本文所使用的<<< <序列> 相同,但是后面的标识符包含在单引号中,例如<<<'EOT'。在nowdoc中没有进行解析。
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 page has 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(对底部的无效优化,C节)起的同样快速的一种方式。另外,这个基准页面有一个单独的vs双引号比较。大多数比较都是一样的。有一种比较,双引号比单引号慢。
#2
170
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.
#3
35
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?
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 is any variables in there.
PHP不会使用额外的处理来解释单引号内的内容。当你使用双引号时,PHP必须进行解析以检查是否存在任何变量。
#4
34
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.
单引号的字符串在运行时更快,因为它们不需要解析。
#5
21
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”、“我的名字”、“abc xyz”,而使用双引号来定义字符串包含“$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.
这对于字符串的其他使用是正确的。
#6
12
Example of single, double, heredoc, and nowdoc quotes
<?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;
?>
#7
11
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.
两种封闭的字符都是字符串。一种类型的报价,很方便地用来夹附另一种类型的报价。“”和“”。引号类型之间最大的区别是,封闭的标识符引用被替换为双引号,而不是在单引号内。
#8
11
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 $test variable.)
这里,双引号解析值,单引号被视为字符串值(不解析$test变量)。
#9
0
Maybe I'm a little late, and a little off-topic, but here it is anyway…
也许我有点晚了,有点跑题了,但不管怎样……
You don't have to choose because of your string's content between:alert("It's \"game\" time.");
or alert('It\'s "game" time.');
您不必选择,因为您的字符串的内容在:警告(“它是\”游戏\“时间”);或警报(‘它’s“游戏”时间。');
Instead, you can type like this, and then use either double or single quotes, because it won't matter:alert("It’s “game” time.");
and alert('It’s “game” time.');
相反,您可以这样键入,然后使用双引号或单引号,因为它不重要:alert(“它是”游戏“时间”);和警报('这“游戏”时间。');
#10
0
$name = 'test';
$singleQuote = 'This is example for single quote $name'; // here $name variable not evaluating
echo $singleQuote; // Output: This is example for single quote $name
$singleQuote = "This is example for single quote $name"; // here $name variable will evaluate and replace variable value
echo $singleQuote; // Output: This is example for single quote test
Also inside single quote expression evaluate faster campare to double quotes
#11
-1
One thing:
一件事:
It is very important to note that the line with the closing identifier of Heredoc must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
需要注意的是,与此协议的结尾标识符的行必须包含除分号(;)之外的其他字符。这意味着标识符可能不会缩进,而且在分号之前或之后可能不会有任何空格或制表符。
Example:
例子:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
#1
905
PHP strings can be specified not just in two ways, but in four ways.
PHP字符串不仅可以通过两种方式指定,还可以通过四种方式来指定。
-
Single quoted strings will 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 strings will 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 what 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 parsing to see how to use array variables and such. - 双引号字符串将显示一组转义字符(包括一些regex),字符串中的变量将被计算。这里的一个重点是,您可以使用花括号来隔离要评估的变量的名称。例如,假设您有一个变量$type,并且您要响应“$types”,它将查找变量$类型。要绕过这个使用echo,“{$type}”是“您可以将左括号放在美元符号之前或之后”。看看字符串解析,看看如何使用数组变量。
-
Heredoc string 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. - Nowdoc(因为PHP 5.3.0)的字符串语法本质上类似于单引号字符串。不同之处在于,即使单引号或反斜杠也不能被转义。现在,一个nowdoc被标识为与本文所使用的<<< <序列> 相同,但是后面的标识符包含在单引号中,例如<<<'EOT'。在nowdoc中没有进行解析。
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 page has 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(对底部的无效优化,C节)起的同样快速的一种方式。另外,这个基准页面有一个单独的vs双引号比较。大多数比较都是一样的。有一种比较,双引号比单引号慢。
#2
170
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.
#3
35
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?
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 is any variables in there.
PHP不会使用额外的处理来解释单引号内的内容。当你使用双引号时,PHP必须进行解析以检查是否存在任何变量。
#4
34
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.
单引号的字符串在运行时更快,因为它们不需要解析。
#5
21
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”、“我的名字”、“abc xyz”,而使用双引号来定义字符串包含“$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.
这对于字符串的其他使用是正确的。
#6
12
Example of single, double, heredoc, and nowdoc quotes
<?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;
?>
#7
11
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.
两种封闭的字符都是字符串。一种类型的报价,很方便地用来夹附另一种类型的报价。“”和“”。引号类型之间最大的区别是,封闭的标识符引用被替换为双引号,而不是在单引号内。
#8
11
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 $test variable.)
这里,双引号解析值,单引号被视为字符串值(不解析$test变量)。
#9
0
Maybe I'm a little late, and a little off-topic, but here it is anyway…
也许我有点晚了,有点跑题了,但不管怎样……
You don't have to choose because of your string's content between:alert("It's \"game\" time.");
or alert('It\'s "game" time.');
您不必选择,因为您的字符串的内容在:警告(“它是\”游戏\“时间”);或警报(‘它’s“游戏”时间。');
Instead, you can type like this, and then use either double or single quotes, because it won't matter:alert("It’s “game” time.");
and alert('It’s “game” time.');
相反,您可以这样键入,然后使用双引号或单引号,因为它不重要:alert(“它是”游戏“时间”);和警报('这“游戏”时间。');
#10
0
$name = 'test';
$singleQuote = 'This is example for single quote $name'; // here $name variable not evaluating
echo $singleQuote; // Output: This is example for single quote $name
$singleQuote = "This is example for single quote $name"; // here $name variable will evaluate and replace variable value
echo $singleQuote; // Output: This is example for single quote test
Also inside single quote expression evaluate faster campare to double quotes
#11
-1
One thing:
一件事:
It is very important to note that the line with the closing identifier of Heredoc must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon.
需要注意的是,与此协议的结尾标识符的行必须包含除分号(;)之外的其他字符。这意味着标识符可能不会缩进,而且在分号之前或之后可能不会有任何空格或制表符。
Example:
例子:
$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;