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语言中,如果它是单引号,这意味着它是一个字符,而不是一个字符串。
10 个解决方案
#1
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. -
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.
单引号字符串几乎完全“按原样”显示。变量和大多数转义序列都不会被解释。例外情况是,要显示文字单引号,您可以使用反斜杠转义它,并显示反斜杠,您可以使用另一个反斜杠\\来转义它(所以是的,即使单引号字符串也会被解析)。
双引号字符串将显示一系列转义字符(包括一些正则表达式),并且将评估字符串中的变量。这里重要的一点是,您可以使用花括号来隔离要评估的变量的名称。例如,假设你有变量$ type,你可以回应“$ types are”这将寻找变量$ types。为了解决这个问题,请使用echo“{$ type} s”你可以在美元符号之前或之后放置左括号。看一下字符串解析,看看如何使用数组变量等。
Heredoc字符串语法的作用类似于双引号字符串。它以<< <开头。在此运算符之后,提供标识符,然后提供换行符。字符串本身跟随,然后再次使用相同的标识符来关闭引号。您不需要在此语法中转义引号。< p>
Nowdoc(自PHP 5.3.0开始)字符串语法基本上类似于单引号字符串。不同之处在于,甚至不需要转义单引号或反斜杠。 nowdoc用与用于heredocs相同的<< <序列标识,但是后面的标识符用单引号括起来,例如, <<< 'eot'。在nowdoc中没有解析。< p>
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部分)。此外,该基准测试页面还有单引号和双引号比较。大多数比较是相同的。有一个比较,其中双引号比单引号慢。
#2
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
'
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 are any variables within the string.
PHP不会使用额外的处理来解释单引号内的内容。当你使用双引号时,PHP必须解析以检查字符串中是否有任何变量。
#4
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
In PHP, both 'my name'
and "my name"
are string. You can read more about it at the PHP manual.
在PHP中,“我的名字”和“我的名字”都是字符串。您可以在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.
对于其他使用的字符串也是如此。
#6
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
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 variableecho '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变量。)
#8
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.
两种封闭的字符都是字符串。一种报价方便地用于包含其他类型的报价。引号类型之间的最大区别在于封闭的标识符引用替换为内部双引号,但不在单引号内。
#9
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.');
你不必选择因为你的字符串之间的内容:alert(“它是\”游戏\“时间。”);或警告('它是'游戏'的时间。');
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.');
相反,你可以这样输入,然后使用双引号或单引号,因为它无关紧要:警告(“它是”游戏“时间。”);和警报('它是'游戏'时间。');
#10
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.
请注意,除了分号(;)之外,具有Heredoc结束标识符的行必须不包含其他字符。这尤其意味着标识符可能没有缩进,并且在分号之前或之后可能没有任何空格或制表符。
Example:
$str = <<<EODExample of stringspanning multiple linesusing heredoc syntax.EOD;
#1
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. -
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.
单引号字符串几乎完全“按原样”显示。变量和大多数转义序列都不会被解释。例外情况是,要显示文字单引号,您可以使用反斜杠转义它,并显示反斜杠,您可以使用另一个反斜杠\\来转义它(所以是的,即使单引号字符串也会被解析)。
双引号字符串将显示一系列转义字符(包括一些正则表达式),并且将评估字符串中的变量。这里重要的一点是,您可以使用花括号来隔离要评估的变量的名称。例如,假设你有变量$ type,你可以回应“$ types are”这将寻找变量$ types。为了解决这个问题,请使用echo“{$ type} s”你可以在美元符号之前或之后放置左括号。看一下字符串解析,看看如何使用数组变量等。
Heredoc字符串语法的作用类似于双引号字符串。它以<< <开头。在此运算符之后,提供标识符,然后提供换行符。字符串本身跟随,然后再次使用相同的标识符来关闭引号。您不需要在此语法中转义引号。< p>
Nowdoc(自PHP 5.3.0开始)字符串语法基本上类似于单引号字符串。不同之处在于,甚至不需要转义单引号或反斜杠。 nowdoc用与用于heredocs相同的<< <序列标识,但是后面的标识符用单引号括起来,例如, <<< 'eot'。在nowdoc中没有解析。< p>
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部分)。此外,该基准测试页面还有单引号和双引号比较。大多数比较是相同的。有一个比较,其中双引号比单引号慢。
#2
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
'
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 are any variables within the string.
PHP不会使用额外的处理来解释单引号内的内容。当你使用双引号时,PHP必须解析以检查字符串中是否有任何变量。
#4
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
In PHP, both 'my name'
and "my name"
are string. You can read more about it at the PHP manual.
在PHP中,“我的名字”和“我的名字”都是字符串。您可以在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.
对于其他使用的字符串也是如此。
#6
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
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 variableecho '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变量。)
#8
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.
两种封闭的字符都是字符串。一种报价方便地用于包含其他类型的报价。引号类型之间的最大区别在于封闭的标识符引用替换为内部双引号,但不在单引号内。
#9
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.');
你不必选择因为你的字符串之间的内容:alert(“它是\”游戏\“时间。”);或警告('它是'游戏'的时间。');
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.');
相反,你可以这样输入,然后使用双引号或单引号,因为它无关紧要:警告(“它是”游戏“时间。”);和警报('它是'游戏'时间。');
#10
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.
请注意,除了分号(;)之外,具有Heredoc结束标识符的行必须不包含其他字符。这尤其意味着标识符可能没有缩进,并且在分号之前或之后可能没有任何空格或制表符。
Example:
$str = <<<EODExample of stringspanning multiple linesusing heredoc syntax.EOD;