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!

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

In this case:

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

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


No related content found.

Leave a comment ?


Leave a Comment