I see this in several third-party classes.
Remembering that closing is usually EOH
, but without <<<
I see this in several third-party classes.
Remembering that closing is usually EOH
, but without <<<
Like most languages, PHP allows you to define strings , that is, strings literally, starting with quotation marks ( "
), or per apex ( '
) - that some call it an apostrophe, single quote, or single quotation mark.
In addition to these standard string definitions, PHP also allows multi-line declarations in Heredoc formats, and since PHP 5.3 in the format Nowdoc , which is very similar, as we will see.
The Heredoc and Newdoc formats are also good for improving readability of strings that need both single and double quotation marks. In addition, it is worth remembering that PHP also allows multi-line strings using quotation marks, unlike many languages.
The Heredoc 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;
Heredoc is indicated by <<<
followed by an identifier, at the user's choice. However, when choosing this identifier, it is appropriate that it be a string that does not occur within the text to be delimited, as the next occurrence of the string at the beginning of the line may confuse the parser.
In any case, the final identifier must happen alone on a single line, and contain only a ;
before the line break.
Here is a snippet of code illustrating 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 \ n on the second line converted to a line break (and then 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, Heredoc has accepted double quotation marks in the handle. Read more below.
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 enclosed in single quotation marks. The basic difference is that Nowdoc interprets the content literally, not replacing the special characters or variables. It is equivalent to the strings delimited with the apex ( '
).
Here's a code snippet very similar to 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 \n
originals.
See more about strings , Heredoc and Nowdoc in #
Related question:
Demonstration in IDEONE courtesy of @GuilhermeNascimento :
This is a method called HEREDOC is an alternative so you do not have to use double quotes to write across multiple lines. See here is equivalent to @"Texto"
of C#
Article from the Wikipedia about: "is a file literal or input stream literal: it is a section of ", translating: This is a way to treat a piece of code as if it were another file, also known as literal .