Strings Heredoc Nowdoc

Posted in PHP by

PHP Heredoc enables to mix php and html in a good to read way. It enables the output of strings without escaping each of them. Nowdoc was introduced with php 5.3 and outputs textual content without any parsing.


textual content inside here.
feel free to use tags, single and double quotes.
$name will output whatever is inside the variable.
Above is an example for a more complex element.
You can write any php inside of the curly braces,
as long as it starts with a dollar sign.


echo <<<'SOME_STRING'
Whatever you write in here. 
It will be not be touched by the php interpreter.
Any php code or variable in here, 
will just be send used as it is,
it will not get parsed.

Useful Hints

  • Nowdoc uses single quotes around the string.
  • The string can be anything, but it can not be inside the text.
  • Heredoc and Nowdoc are closed by "YOUR_STRING;". It must be the first element in the line. No spaces or tabs allowed.
  • The close statement can not be the last php statement in a file, otherwise you will get a parse error. (use ?>)
  • You can not use calls to static functions or classes inside of the curly braces, because the first char after the left curly brace must be the dollar sign.
  • Take a look at XHP. It is a lot better.

Published at , Updated at 2011-11-06

next: autoloader prev: MVC Model View Controller