Tag Archives: multiple lines in PHP variable using heredoc

PHP Heredoc example

Its like method to create string in PHP with multiple lines and almost anything in it, but without using quotations.

Example 1:

<?php
$heredocstr = <<<ANYTHING
Here you can type anything as heredoc string
Use multiple lines if required.
ANYTHING; // Closing heredoc identifier

echo $heredocstr;
?>

Caution: There should be no space or words before this word ANYTHING in the closing heredoc identifier, otherwise PARSE ERROR will be generated

heredoc starts with the <<< operator and an identifier, here ANYTHING as identifier. Start to type your text in more lines as a double quoted string. You can also use variables inside the string. To close the string write the identifier ANYTHING again in a new line as above:

Example 2:

<?php
$heredocstr = <<<ANYTHING
Here you can type anything as heredoc string
Use multiple lines if required.
You can use any $variable in heredoc string
ANYTHING; // There should be no space or words before this word ANYTHING

echo $heredocstr;
?>