PHP: Heredoc and Nowdoc String Syntax
What is Heredoc
heredoc is a string syntax suitable for quoting large text with multiple lines.
Heredoc is especially useful for large block of text (such as a template) that may itself contain many APOSTROPHE or QUOTATION MARK .
Syntax
start the quote delimiter by
<<<anyLetterSequence
and end it by the same anyLetterSequence , followed by a semicolon, on a line by itself.
Variables inside heredoc are replaced by their values.
Same for escape sequences (e.g. \n
, \t
etc.).
<?php $ss = 9; $xx = <<<hhhaaa i have $ss cats, and something. hhhaaa; echo $xx; # i have 9 cats, # and something. ?>
Nowdoc: Literal Quoting Long Text
To not have variables interpreted, use a form called nowdoc.
The syntax is the same as heredoc, except bracket the random string with APOSTROPHE.
<<<'anyLetterSequence'
<?php $herName = 'Alice'; $longText = <<<'ttthhhmmm' 'and what is the use of a book,' thought $herName 'without pictures or conversation?'. ttthhhmmm; echo $longText; /* 'and what is the use of a book,' thought $herName 'without pictures or conversation?'. */ ?>