PHP heredoc syntax <<<

Sometimes you need to use long strings (usually HTML code) in variable assignment or simply to echo them.  There are several options to delimit string, one of which is using the heredoc syntax:

use “<<<” then any identifier you wish to use (PHP naming rules), new line, the string for use (across multiple lines), new line, closing identifier.  See below:

 

After the <<< operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line. Also, the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.

Leave a Reply

Your email address will not be published. Required fields are marked *