Php 简明教程

PHP - Heredoc & Nowdoc

PHP 提供了两种替代方法,可以使用 heredocnewdoc 语法来声明单引号或双引号字符串。

PHP provides two alternatives for declaring single or double quoted strings in the form of heredoc and newdoc syntax.

  1. The single quoted string doesn’t interpret the escape characters and doesn’t expand the variables.

  2. On the other hand, if you declare a double quoted string that contains a double quote character itself, you need to escape it by the "\" symbol. The heredoc syntax provides a convenient method.

Heredoc Strings in PHP

PHP 中的 heredoc 字符串非常像双引号字符串,但没有双引号。这意味着它们不需要转义引号和展开变量。

The heredoc strings in PHP are much like double-quoted strings, without the double-quotes. It means that they don’t need to escape quotes and expand variables.

Heredoc Syntax

$str = <<<IDENTIFIER
place a string here
it can span multiple lines
and include single quote ' and double quotes "
IDENTIFIER;

首先,从 "<<<" 运算符开始。在此运算符之后,提供一个标识符,然后换行。字符串本身紧随其后,然后再次使用相同的标识符来结束引号。该字符串可以跨越多行,并包括单引号 (') 或双引号 (")。

First, start with the "<<<" operator. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation. The string can span multiple lines and includes single quotes (‘) or double quotes (").

结尾标识符可以通过空格或制表符缩进,在这种情况下,缩进将从文档字符串中的所有行中删除。

The closing identifier may be indented by space or tab, in which case the indentation will be stripped from all lines in the doc string.

Example

该标识符只能包含字母数字字符和下划线,并且以下划线或非数字字符开头。结尾标识符不应包含分号 (;) 以外的任何其他字符。此外,结尾标识符前后必须只能是换行符。

The identifier must contain only alphanumeric characters and underscores and start with an underscore or a non-digit character. The closing identifier should not contain any other characters except a semicolon (;). Furthermore, the character before and after the closing identifier must be a newline character only.

请看以下示例:

Take a look at the following example −

<?php
   $str1 = <<<STRING
   Hello World
      PHP Tutorial
         by TutorialsPoint
   STRING;

   echo $str1;
?>

它将生成以下 output

It will produce the following output

Hello World
    PHP Tutorial
        by TutorialsPoint

Example

结尾标识符在编辑器的第一列之后可能包含缩进,也可能不包含缩进。如果有缩进,它将被删除。但是,结尾标识符的缩进不能比正文中的任何行更深。否则,将引发 ParseError。请看以下示例及其输出 −

The closing identifier may or may not contain indentation after the first column in the editor. Indentation, if any, will be stripped off. However, the closing identifier must not be indented further than any lines of the body. Otherwise, a ParseError will be raised. Take a look at the following example and its output −

<?php
   $str1 = <<<STRING
   Hello World
      PHP Tutorial
   by TutorialsPoint
         STRING;

   echo $str1;
?>

它将生成以下 output

It will produce the following output

PHP Parse error:  Invalid body indentation level
(expecting an indentation level of at least 16) in hello.php on line 3

Example

heredoc 中的引号不需要转义,但仍可以使用 PHP 转义序列。Heredoc 语法还会展开变量。

The quotes in a heredoc do not need to be escaped, but the PHP escape sequences can still be used. Heredoc syntax also expands the variables.

<?php
   $lang="PHP";
   echo <<<EOS
   Heredoc strings in $lang expand vriables.
   The escape sequences are also interpreted.
   Here, the hexdecimal ASCII characters produce \x50\x48\x50
   EOS;
?>

它将生成以下 output

It will produce the following output

Heredoc strings in PHP expand vriables.
The escape sequences are also interpreted.
Here, the hexdecimal ASCII characters produce PHP

Nowdoc Strings in PHP

PHP 中的 Nowdoc 字符串与 Hereodoc 字符串类似,不同之处在于它不扩展变量,也不解释转义序列。

A nowdoc string in PHP is similar to a heredoc string except that it doesn’t expand the variables, neither does it interpret the escape sequences.

<?php
   $lang="PHP";

   $str = <<<'IDENTIFIER'
   This is an example of Nowdoc string.
   it can span multiple lines
   and include single quote ' and double quotes "
   IT doesn't expand the value of $lang variable
   IDENTIFIER;

   echo $str;
?>

它将生成以下 output

It will produce the following output

This is an example of Nowdoc string.
it can span multiple lines
and include single quote ' and double quotes "
IT doesn't expand the value of $lang variable

Nowdoc 的语法类似于 Hereodoc 的语法,不同之处在于,遵循 "<<<" 运算符的标识符需要用单引号引起来。Nowdoc 的标识符也遵循 Hereodoc 标识符的规则。

The nowdoc’s syntax is similar to the heredoc’s syntax except that the identifier which follows the "<<<" operator needs to be enclosed in single quotes. The nowdoc’s identifier also follows the rules for the heredoc identifier.

Heredoc 字符串类似于不带转义的双引号字符串。Nowdoc 字符串类似于不带转义的单引号字符串。

Heredoc strings are like double-quoted strings without escaping. Nowdoc strings are like single-quoted strings without escaping.