Nowdoc and heredoc in PHP

From the manual:

Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping.

Look that the difference between those 2 is that nowdocs uses single quotes when defining the tag (that can be anything you want) and heredocs doesn’t.

$foo = 'bar';

$now = <<<'NOW'
    I'm now, $foo!
NOW;

$here = <<<HERE
    I'm here, $foo!
HERE;

In this case:

$now is "I'm now, $foo!"

$here is "I'm here, bar!"

Ref: http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc
http://stackoverflow.com/a/11153164

You might also like

Unserialize strings with special characters in PHP
After several hours having problems with serialized object with spanish texts in it, finally this function...

Fix date strings with percentage symbols in qtranslate
If you see dates with percentage symbols when using qtranslate in WordPress:You can fix it by...

Twig-extensions in Symfony2
Twig comes with multiple filters that provide functions like round, slice, sort, etc. In case you need...

Select different available java and javac versions in Ubuntu with update-alternatives
It's possible to have more than one version of Java in the same machine.If you have more than...

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.