PHP中的单引号和双引号的字符串有什么区别?

发布于 2025-02-04 07:41:29 字数 94 浏览 3 评论 0原文

我有点困惑,为什么我会在PHP中看到一些代码,其中有单个引号,有时用双引号放置。

我只知道在.NET或C语言中,如果是单语言,则意味着它是字符,而不是字符串。

I'm a little confused why I see some code in PHP with string placed in single quotes and sometimes in double quotes.

I just know in .NET, or the C language, if it is in a single quote, that means it is a character, not a string.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

挖鼻大婶 2025-02-11 07:41:30

两种封闭的字符都是字符串。一种类型的报价可方便地用来包装另一种类型的报价。 “'”'“''。引号类型之间的最大区别是,封闭式标识符引用被用代替内部双引号,而不是在单个引号中。

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.

夕色琉璃 2025-02-11 07:41:30

有些人可能会说我有点偏离主题,但是无论如何:

您不一定要选择字符串的内容:
echo“是\”游戏\“时间。”;echo'it \'s“ game” time。';

如果您熟悉使用英文引号和撇号的正确字符,您可以使用双引号或单个引号,因为它不再重要:
echo“是“游戏”时间。”;echo'这是“游戏”时间。';

当然,您还可以在需要时添加变量。只是不要忘记,只有在双引号中才能评估它们!

Some might say that I'm a little off-topic, but here it is anyway:

You don't necessarily have to choose because of your string's content between:
echo "It's \"game\" time."; or echo 'It\'s "game" time.';

If you're familiar with the use of the english quotation marks, and the correct character for the apostrophe, you can use either double or single quotes, because it won't matter anymore:
echo "It’s “game” time."; and echo 'It’s “game” time.';

Of course you can also add variables if needed. Just don't forget that they get evaluated only when in double quotes!

暮倦 2025-02-11 07:41:29

php strings 不仅可以指定两种方式,但以四种方式。

  1. 几乎将完全显示“原样”。变量和大多数逃生序列将无法解释。例外是要显示一个字面的单报价,您可以使用后斜线\'逃脱它,并且要显示后斜线,您可以使用另一个后斜线\\ (是的,即使是单个引用的字符串也被解析)。
  2. 将显示许多逃生序列(包括一些重视),并将评估字符串中的变量。这里的重要一点是,您可以使用卷曲支架来隔离所需评估的变量的名称。例如,假设您具有变量$ type,并且要echo“ $ type是”。这将寻找变量$ types。要解决此使用echo“ {$ type} s是”。看看查看如何使用数组变量等。
  3. heredoc 字符串语法就像双引号字符串一样。它以<<<开始。在此操作员之后,提供了标识符,然后提供了一个newline。字符串本身遵循,然后再次使用相同的标识符来关闭报价。您无需在本语法中逃脱引号。
  4. (由于PHP 5.3.0)字符串语法本质上类似于单引号字符串。不同之处在于,即使是单引号或反斜线也不必须逃脱。 nowdoc用相同的&lt;&lt;&lt;序列用于Heredocs,但是随后的标识符以单引号包含,例如&lt;&lt;&lt;'eot'< /代码>。 在Nowdoc中未完成解析。

注意:
必须逃脱单语引号内的单引号和双引号的双引号:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

速度:
没有差异
请阅读a

for($i=0;$i<100000;$i++) {
    'string';
}

引用的字符串仅被解析曾经以及整个脚本,然后将其翻译成OpCode。然后将执行一百万次。因此,除了解析外,它都可以衡量任何东西。那只是冰山一角。对于这样的纳米计算,几乎不可能创建一个可靠的测试,而这些测试不会因某些干扰的副作用而破坏。

PHP strings can be specified not just in two ways, but in four ways.

  1. 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).
  2. Double quote strings will display a host of escape sequences (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". Take a look at string parsing to see how to use array variables and such.
  3. 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.
  4. 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.

Notes:
Single quotes inside of single quotes and double quotes inside of double quotes must be escaped:

$string = 'He said "What\'s up?"';
$string = "He said \"What's up?\"";

Speed:
There is no difference.
Please read a credible article on the matter from one of PHP core developers. Speaking of tests, one should never take them for granted. It must be understood that writing a credible test and, especially, interpreting its results requires a lot of knowledge and experience. Which means that most tests out there are just bogus. For example, in a code like this

for($i=0;$i<100000;$i++) {
    'string';
}

The quoted string gets parsed only once, along with entire script, and then gets translated into opcode. Which is then gets executed a million times. So it measures anything but parsing. And that's only a tip of the iceberg. For a nanobenchmark like this, it's practically impossible to create a credible test that wouldn't be spoiled by some interfering side effect.

你好,陌生人 2025-02-11 07:41:29

事情以双引号进行评估,但不会单一评估:

$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.

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.
む无字情书 2025-02-11 07:41:29

'单人引用

指定字符串的最简单方法是将其包装在单引号中。单引号中的所有内容都被视为平弦。

示例:

echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';

双引号中的双引号插值变量和广泛的逃生序列。注意:使用Curly Braces {}包括复杂 下一个字符是变量名称的有效

echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";

字符

变量或在变量

之后的 一次解析整个代码,然后将结果字节码存储在

' Single quoted

The simplest way to specify a string is to enclose it in single quotes. Everything inside single quotes is treated as a plain string.

Example:

echo 'Start with a simple string';
echo 'Variables $var and escape sequences \n do not get interpolated';
echo 'You only need to escape the quote \' and a backslash itself\\';

" Double quoted

Strings in double quotes interpolate variables and a wide range of escape sequences. Note: Use curly braces {} to include complex variables or in case when next character after a variable is a valid character for a variable name.

Example:

echo "Start with a simple string";
echo "String's apostrophe";
echo "Escape sequences get interpolated\n";
echo "Variables $var get interpolated as well {$name}_sake";

Is there a performance benefit single quote vs double quote in PHP?

No.

In order to improve performance, PHP parses the entire code once and then stores the resulting bytecode in the opcode cache. This approach eliminates the entire parsing, along with whatever quoited strings as well.

笨笨の傻瓜 2025-02-11 07:41:29

单引号的字符串没有解释其中的变量。双引号的字符串确实可以。

另外,双引号的字符串可以包含无反冲的撇号,而单引号的字符串可以包含未示例的引号。

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.

节枝 2025-02-11 07:41:29

在PHP中,'我的名字'“我的名字”是字符串。您可以在php手册上阅读更多有关它的信息

您应该知道的是

$a = 'name';
$b = "my $a"; == 'my name'
$c = 'my $a'; != 'my name'

PHP中,人们使用单个报价来定义一个常数字符串,例如'a'''我的名字''abc xyz'< /code>,在使用双引号定义字符串时包含标识符时,例如“ a $ b $ c $ d”

In PHP, both 'my name' and "my name" are string. You can read more about it at the PHP manual.

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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文