php - 有没有另一种方法可以在 PHP 中编写字符串文字(没有 ' or ")?

标签 php string symbols

我可以在 PHP 中使用什么来代替正常的 ' 和(没有 ' 或 ")符号?

示例:

echo("Hello, World!")

最佳答案

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

阅读full php.net article here .

定理

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.

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;
<小时/>

现在文档

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.

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;
<小时/>

转义

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

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

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

<小时/>

如果您必须大规模添加引号,请使用 addslashes()功能:

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

关于php - 有没有另一种方法可以在 PHP 中编写字符串文字(没有 ' or ")?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10886899/

相关文章:

c - 链接时加载符号文件

php - 展开列以执行 SQL 搜索查询

java - 如何将字符串值转换为数组?

c++ - 删除在 C++ 中使用 malloc 分配的 std::string 数组

静态文件中的 C 符号可见性

character-encoding - gnuplot 中的星号

php - 有什么方法可以阻止 GET 方法表单发送空变量吗?

php - 如何将变量绑定(bind)到闭包?

php - 如何在使用 php 和 mysql 驱动数据库的网站中查看用户是否在线?

c++ - 如何使用变量名引用数组位置