What does <<< EOH in PHP do?

Asked

Viewed 1,282 times

31

I see it in several third-party classes.

Remembering that the closure is usually the EOH, but without <<<

2 answers

29


Definition of strings in PHP

Like most languages, PHP allows you to define strings, i.e., strings of characters literally, starting with quotation marks (") , or per apex (') - which some call simple quote.

In addition to these normal string definitions, the PHP also allows multi-line statements in formats Heredoc, and since the PHP 5.3 in format Nowdoc, which is very similar, as we will see.

The formats Heredoc and Newdoc are also good for improving the readability of strings that need to have both single and double quotes. Additionally, it is good to remember that the PHP also allows multi-line strings using quotes, unlike many languages.


Syntax Heredoc:

Heredoc’s syntax is this:

construct/variavel <<<IDENTIFICADOR
Aqui vão as linhas que
devem ser armazenadas na string.
Podemos incluir $variaveis, "aspas" e... \t caracteres especiais.
IDENTIFICADOR;

The Heredoc is indicated by <<< followed by an identifier, at the user’s choice. However, when choosing this identifier, it is appropriate that it is a string that does not occur within the text that will be delimited, because the next occurrence of the string at the beginning of the line may confuse the parser.

In any case, the final identifier should happen alone on a single line, and contain only one ; before the line breaks.

Here is an excerpt of code exemplifying its use:

$nome = "Leandro";
$texto = <<<BATATINHAS
Olá, $nome. Este é um exemplo prático do Heredoc.
\nNeste caso, usamos o "Heredoc" com quebra de linha com
barra invertida, e troca da variável "nome".
BATATINHAS;
echo nl2br( htmlentities( $texto ) );

And its output, seen in the browser, would be:

Olá, Leandro. Este é um exemplo prático do Heredoc.

Neste caso, usamos o "Heredoc" com quebra de linha com
barra invertida, e troca da variável "nome".

Note that the $name variable has been replaced by its value, and the n in the second line converted to a line break (and then by a <br> thanks to nl2br, but this is already another topic). It would be comparable to a string delimited with ".

Note: since the introduction of Nowdoc in PHP 5.3, the Heredoc started to accept double quotes in the identifier. Read more below.


Syntax Nowdoc:

The syntax of Nowdoc is very similar to that of Heredoc, was added in PHP 5.3 as an alternative to Heredoc:

construct/variavel <<<'IDENTIFICADOR'
Aqui vão as linhas que
devem ser armazenadas na string.
Aqui já não serao substituidos $variaveis e \t caracteres especiais.
IDENTIFICADOR;

What changes is the identifier, which this time is placed between single quotes. The basic difference is that the Nowdoc interprets the content literally, not substituting special or variable characters. It is equivalent to strings delimited with the apex (').

Here’s a code snippet very similar to the Heredoc, exemplifying its use:

$nome = "Leandro";
$texto = <<<'BATATINHAS'
Olá, $nome. Este é um exemplo prático do Nowdoc.
\nNeste caso, usamos o "Nowdoc" para mostrar que
não acontece substituição de caracteres.
BATATINHAS;
echo nl2br( htmlentities( $texto ) );

And its output, seen in the browser, would be:

Olá, $nome. Este é um exemplo prático do Nowdoc.
\nNeste caso, usamos o "Nowdoc" para mostrar que
não acontece substituição de caracteres.

Note that in this case, the text was identical to the initial declaration, preserving the string $nome and the \n originals.

See more about strings, Heredoc and Nowdoc in the manual of PHP

Related question:

Difference between single and double quotes in PHP

Demonstration at IDEONE, courtesy of @Guillhermenascimento:

https://ideone.com/iXMesy

  • Perfect! It was more than clear.

  • Excellent! Boy you really helped me solved my problem with Newdoc, now I can generate php files dynamically. Thank you very much friend!

  • very good, I think this will help too

11

This is a method called HEREDOC is an alternative to not having to use double quotes to write in multiple lines. See more here is equivalent to @"Texto" of C#

Article of Wikipedia about: "is a file literal or input stream literal: it is a Section of a source code file that is treated as if it Were a Separate file.", translating: It is a way to treat a part of the code as if it were another file, also known as literal.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.