Search This Blog

Loading...

8/22/2012

Heredoc and Nowdoc


Heredoc and Nowdoc (also known as "here document" and "now document") are two alternative representation of strings in PHP (as double quotes and single quotes are).


This post presents these two language features and how to use them.







Heredoc

Heredoc is older than nowdoc. It should be used to create lange blocks of text inside PHP escope, without needed to avoid escaping single or double quotes. Its behavior is similar to doble quote notation, that can receive variables or special sequences (like "\n") inside. Although, you should worry to use de dollar sign ($), that should be escaped.


The syntax is very easy. You have to start the block with three less-than sign (<) followed by an identifier (the identifier can be delimited by double quotes) and followed by a line feed. After the start, you put the string content. The end of block is represented by the identifier alone in a line, followed by the semicolon sign.


Example:

$html = <<<HTML
  <div id="help">
    <p>Hello, {$name}.</p>
    <p>This product costs \$ {$price}.</p>
    <p>To buy this product, click on 'buy' button.</p>
  </div>
HTML;

This code is equivalent to (using single quotes):

$html = '  <div id="help">
    <p>Hello, '.$name.'.</p>
    <p>This product costs $ '.$price.'.</p>
    <p>To buy this product, click on \'buy\' button.</p>
  </div>';

Or (using double quotes):

$html = "  <div id=\"help\">
    <p>Hello, {$name}.</p>
    <p>This product costs \$ {$price}.</p>
    <p>To buy this product, click on 'buy' button.</p>
  </div>";

Note that Heredoc was useful to represent this kind of string that have more than one line. Using single or double quotes, it was necessary to escape the single quotes, double quotes or the dollar sign.





Nowdoc


The Nowdoc notation was introduced in PHP 5.3 and it is similar to Heredoc, but it does not accept variables, nor special sequences like "\n" inside. It is very useful when we need to create a large block of text that has dollar sign that does not represents variables. For exemple, a string that contains a PHP code.


The syntax is similar to Heredoc, except that you should delimit the identifier by single quotes at start of block in the Nowdoc notation. The end of block continues without quotes.


Example:

$php_code = <<<'EOF'
/**
 * Example of function in PHP
 */
public function save($data) {
    ...
}
EOF;

Note that it was not necessary to escape the dollar sign.


Observation


The and of block Heredoc or Nowdoc can not be indented, as the example:


    if ($x == 1) {
        $text = <<<TEXT
<p>bla bla bla</p>
<p>blo blo blo</p>
TEXT;
    }

Yes, it is ugly for somebody, but syntax is syntax, is not it?


You have also to remember include a new line after the end of block (for heredoc or nowdoc).




Conclusion


Heredoc is the most convenient way to create strings with many break lines that could be created with double quotes notation.


Nowdoc is the most convenient way to create strings with many break lines that could be created with single quotes notation.

3 comments:

  1. why not just enclose the php code inside single quotes instead of nowdoc?

    ReplyDelete
  2. Because you may have a single quote or a "$" sign in your php code. Using Nowdoc, you do not have to escape these characteres. Using single quote, you have.

    ReplyDelete
  3. Its Help me thanks dude!!!!!

    ReplyDelete