我有点困惑为什么我在PHP中看到一些字符串放在单引号中,有时在双引号中。
我只知道在。net或C语言中,如果它是单引号,那就意味着它是一个字符,而不是字符串。
我有点困惑为什么我在PHP中看到一些字符串放在单引号中,有时在双引号中。
我只知道在。net或C语言中,如果它是单引号,那就意味着它是一个字符,而不是字符串。
当前回答
这两种括起来的字符都是字符串。一种类型的报价可以方便地在另一种类型的报价中附上。"'"和" " "。这两种引号类型之间最大的区别是,括起来的标识符引用替代了双引号内的标识符引用,而不是单引号内的标识符引用。
其他回答
单引号
指定字符串最简单的方法是用单引号将其括起来。单引号通常更快,其中引用的所有内容都被视为普通字符串。
例子:
echo 'Start with a simple string';
echo 'String\'s apostrophe';
echo 'String with a php variable'.$name;
双引号
在PHP中使用双引号以避免必须使用句号来分隔代码(注意:如果您不想在字符串中使用连接(.)操作符,请使用花括号{}来包含变量)。
例子:
echo "Start with a simple string";
echo "String's apostrophe";
echo "String with a php variable {$name}";
PHP中单引号和双引号在性能上有优势吗?
是的。使用单引号稍微快一些。
PHP不会使用额外的处理来解释单引号内的内容。当您使用双引号时,PHP必须解析以检查字符串中是否有任何变量。
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 want to echo "The $types are". That will look for the variable $types. To get around this use echo "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.
注: 单引号中的单引号和双引号中的双引号必须转义:
$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";
速度: 我不认为单引号比双引号更快。他们可能在某些情况下更快。这里有一篇文章解释了自PHP 4.3以来单引号和双引号本质上一样快的一种方式(底部的无用优化,C节)。此外,这个基准测试页面有单引号和双引号的比较。大多数比较都是一样的。有一个双引号比单引号慢的比较。
单引号字符串中没有解释变量。双引号字符串可以。
同样,双引号字符串可以包含不带反斜杠的撇号,而单引号字符串可以包含未转义的引号。
单引号字符串在运行时更快,因为它们不需要解析。
有些人可能会说我有点跑题,但无论如何,我是这样说的:
你不必因为字符串的内容而选择: echo“游戏时间到了。”;或者呼应“游戏时间到了。”;
如果你熟悉英文引号的使用,并且知道撇号的正确字符,你可以使用双引号或单引号,因为这已经不重要了: 回声“游戏时间到了。”;并呼应“游戏时间到了”;
当然,如果需要,还可以添加变量。别忘了只有在双引号中它们才会被求值!
在PHP中,'my name'和"my name"都是字符串。您可以在PHP手册中阅读更多关于它的内容。
你应该知道的事情
$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'
在PHP中,人们使用单引号来定义常量字符串,如'a', 'my name', 'abc xyz',而使用双引号来定义包含标识符的字符串,如"a $b $c $d"。
还有一件事,
echo 'my name';
比
echo "my name";
but
echo 'my ' . $a;
比
echo "my $a";
字符串的其他用法也是如此。