Heredoc Syntax

Heredoc is a strange but necessary way to create a complex string. The functionality provided is almost identical to double quoted strings except there are no quotes.

The heredoc syntax uses the special operator ‘<<<' followed by a user defined identifier. The same identifier on a new line must be used to close the string followed by an optional semicolon. This identifier is restricted by the same rules as a regular variable name.

  1.  
  2. $what = "Certification";
  3. echo <<<TEXT
  4. Zend PHP "$what"
  5. TEXT;
  6.  

This will output ‘Zend PHP “Certification”‘.
Heredoc syntax can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables.

Leave a Reply

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

*