Вопрос по string, php – Альтернативный способ написания строковых литералов в PHP? (без 'или ")

12

Что я мог бы использовать в php вместо обычных & apos; и & quot; символы вокруг чего-то?

Пример:

echo("Hello World!")

Спасибо!

Error: User Rate Limit Exceeded". Denys Séguret
Error: User Rate Limit Exceeded Jeroen
Error: User Rate Limit Exceeded Sarfraz
Error: User Rate Limit Exceeded user166390
Error: User Rate Limit Exceeded"\"". Denys Séguret

Ваш Ответ

1   ответ
28

Error: User Rate Limit Exceeded'Error: User Rate Limit Exceeded", Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded.

Error: User Rate Limit ExceededError: User Rate Limit Exceeded.

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.

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded

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

Escaping

Error: User Rate Limit Exceeded

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

Error: User Rate Limit Exceeded


Error: User Rate Limit ExceededError: User Rate Limit ExceededError: User Rate Limit Exceeded

$str = "Is your name O'reilly?";
echo addslashes($str); // Is your name O\'reilly?
Error: User Rate Limit Exceeded"O'reilly"Error: User Rate Limit Exceeded'She said, "Me too"'
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded
Error: User Rate Limit Exceeded

Похожие вопросы