有没有其他方法可以在 PHP 中编写字符串文字(没有 ' 或 ")?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10886899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:16:56  来源:igfitidea点击:

Is there an alternative way to write string literals in PHP (without ' or ")?

phpstringsymbols

提问by Jeroen

What could I use in PHP in place of the normal 'and (without ' or ") symbols around something?

我可以在 PHP 中使用什么来代替'某些东西周围的普通和(没有 ' 或 ")符号?

Example:

例子:

echo("Hello, World!")

回答by Jeroen

There are 4 ways to encapsulate strings, single quotes ', double quotes ", heredocand nowdoc.

有 4 种封装字符串的方式,单引号',双引号"heredocnowdoc

Read the full php.net article here.

此处阅读完整的 php.net 文章

Heredoc

赫里多克

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

分隔字符串的第三种方法是heredoc 语法:<<<。在这个运算符之后,提供了一个标识符,然后是一个换行符。紧随其后的是字符串本身,然后再次使用相同的标识符来关闭引号。

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;


Nowdoc

现在文档

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML construct, in that it declares a block of text which is not for parsing.

A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes, e.g. <<<'EOT'. All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.

Nowdocs 是单引号字符串,就像heredocs 是双引号字符串。nowdoc 的指定与 heredoc 类似,但在 nowdoc 中不进行解析。该构造非常适合嵌入 PHP 代码或其他大文本块而无需转义。它与 SGML 构造有一些共同的特征,因为它声明了一个不用于解析的文本块。

nowdoc 用与heredocs 相同的<<< 序列标识,但后面的标识符用单引号括起来,例如<<<'EOT'。Heredoc 标识符的所有规则也适用于 nowdoc 标识符,尤其是关于结束标识符外观的规则。

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

$str = <<<'EOD'
Example of string
spanning multiple lines
using nowdoc syntax.
EOD;


Escaping

逃跑

If you want to use literal single or double quotes within single or double quoted strings, you have to escape them:

如果要在单引号或双引号字符串中使用文字单引号或双引号,则必须对它们进行转义:

$str = '\''; // single quote
$str = "\""; // double quote

As Herbert noted, you don't have to escape single quotes within a double quoted strings and you don't have to escape double quotes within a single quoted string.

正如 Herbert 所指出的,您不必在双引号字符串中转义单引号,也不必在单引号字符串中转义双引号。



If you have to add quotes on a large scale, use the addslashes()function:

如果您必须大规模添加引号,请使用addlashes()函数:

$str = "Is your name O'reilly?";
echo addslashes($str); // Is your name O\'reilly?