PHP Heredoc and Nowdoc Explained

php string

In our phpGrid examples, we’ve seen some code with ‘weird’ syntax such as in event handler example:

1
2
3
4
5
6
7
8
9
10
11
12
13
...

// post data another page after submit
$afterSubmit = <<<AFTERSUBMIT
function (event, status, postData)
{
    phpGrid_orders.trigger("reloadGrid");
}
AFTERSUBMIT
;

$dg->add_event("jqGridInlineAfterSaveRow", $afterSubmit);

...

So what are these and how do they work?

In PHP, there are numerous ways to specify a string value. The two most frequent methods are using single or double quotations.

Double Quotes

With double quoted string, escape characters like \n, is regarded as a new line break and variables are replaced with their values. As in this example:

1
2
3
4
5
6
$hello = 'Hello';
echo "$hello\nWorld!";

// output
// Hello
// World!

Single Quotes

When strings that are single quoted, they are treated as literal, meaning that escaped characters do not expand; for example, ‘\n’ will not create a new line. Variables are not replaced by the values assigned to them.

1
2
3
4
5
$hello = 'Hello';
echo '$hello\nWorld!';

// output
// $hello\nWorld

When we wish to define a multiline string, things become messier. For example, on occasions, we need to include multi-line javascript in PHP such as

1
2
3
4
function foo(status, rowid)
{
    $("#reportsTo").attr("size", 10);
}

Introducing Heredoc and Nowdoc

Luckily, PHP offers a better way to write multiple-line string variables directly with Heredoc and Nowdoc syntax.

The basic rules for Heredoc and Nowdoc are

  • * Starting with a “triple less-sign” before a unique identifier for the beginning and end of the string,
  • * The delimiter must always be at the beginning of a line, without any spaces, letters, or other characters.
  • * The closing identifier must be on a new line, followed by a semi-colon, and with no white space before it.

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
$size = 10;
echo <<<EOT
function foo(status, rowid)
{
    $("#reportsTo").attr("size", $size);
}
EOT
;

// Out put
// function foo(status, rowid)
// {
//  $("#reportsTo").attr("size", 10);
// }

We could use any string to represent identifier to mark the start and end of the string. The triple less sign must always come before the opening identifier.

Heredoc differs from Nowdoc in that it makes use of double-quoted strings. For escape sequences, etc., parsing is performed inside a heredoc, but a nowdoc employs single-quoted texts and hence parsing is not performed.

1
2
3
4
5
6
7
8
9
10
11
12
13
$size = 10;
echo <<<'EOT'
function foo(status, rowid)
{
    $("#reportsTo").attr("size", $size);
}
EOT
;

// Output:
// function foo(status, rowid)
// {
//  $("#reportsTo").attr("size", $size);
// }

Conclusion

Heredoc and nowdoc are handy alternatives to the more frequently used quoted string syntax in PHP for creating strings, especially string that spans multiple lines.

So the next time when you dealing with a long string, try heredoc or nowdoc!