31
I see it in several third-party classes.
Remembering that the closure is usually the EOH
, but without <<<
31
I see it in several third-party classes.
Remembering that the closure is usually the EOH
, but without <<<
29
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.
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.
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:
Demonstration at IDEONE, courtesy of @Guillhermenascimento:
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.
Great! Thank you for the reply.
Browser other questions tagged php characteristic-language heredoc
You are not signed in. Login or sign up in order to post.
Perfect! It was more than clear.
– Leandro Curioso
Excellent! Boy you really helped me solved my problem with Newdoc, now I can generate php files dynamically. Thank you very much friend!
– Leandro Curioso
very good, I think this will help too
– flourigh