Home  >  Article  >  New features of PHP7.3: flexible heredoc and nowdoc syntax structures will be ushered in

New features of PHP7.3: flexible heredoc and nowdoc syntax structures will be ushered in

无忌哥哥
无忌哥哥Original
2018-07-12 10:43:192758browse

New features of PHP7.3: flexible heredoc and nowdoc syntax structures will be ushered in

The php.net RFC channel has announced the Heredoc and Nowdoc syntax updates for PHP 7.3. This update focuses on code readability:

Heredoc and Nowdoc has a very strict syntax, which sometimes makes many developers avoid it because they look very ugly in the code, making the code less readable. In response to this problem, this update has made the following two changes to the syntax:

  1. supports the indentation of closing tags;

  2. Line breaks for closing tags are no longer forced;

## From the current implementation of PHP 7.2, like this simple example:

<?php
class foo {
    public $bar = <<<EOT
bar
EOT;
}
in version 7.3 , the following forms are available:

<?php
class foo {
    public $bar = <<<EOT
    bar
    EOT;
}
The indentation of the closing tag determines the number of spaces per new line in heredoc/nowdoc:

<?php

// 4 个缩进空格
echo <<<END
      a
     b
    c
    END;
/*
  a
 b
c
*/
In the current implementation of PHP 7.2, A newline must exist to end heredoc/nowdoc. PHP 7.3 removes this constraint:

<?php

stringManipulator(<<<END
   a
  b
 c
END);

$values = [<<<END
a
b
c
END, 'd e f'];
Background of Heredoc and Nowdoc

Nowdoc is supported from PHP 5.3.0 version. The only difference between it and Heredoc is double quotes and single quotes. The difference between quotation marks. Nowdoc adds single quotes around the start tag, and there is no parsing:

<?php

$name = 'Example';
$str = <<<'EOD'
Example of string $name
spanning multiple lines
using nowdoc syntax.
EOD;
The above nowdoc will output:

Example of string $name
spanning multiple lines
using nowdoc syntax.
Here The definition of the document on the wiki:

In computer science, a here document, also known as a heredoc, hereis, here-string or here-script, is a file input or data stream input: a block of code that can be treated as a complete file. It can save whitespace characters such as line breaks or indents in the text. Some languages ​​allow variable substitution and command substitution within strings.

Improvements in Heredocs and Nowdocs will make your PHP code more readable and error-prone. On the other hand, the output is more concise and direct because the indentation that would close the markup is removed.

Get more information

It is recommended to read the official change document - flexible Heredoc and Nowdoc Syntaxes RFC. PHP official documentation Heredoc and Nowdoc.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn