• Tutorial List

PHP HereDoc, NowDoc, and HereDoc VS NowDoc

Last updated May 15, 2020
PHP HereDoc, NowDoc, and HereDoc VS NowDoc

PHP HereDoc

PHP heredoc is another way to create strings with a large amount of text without using quotations.


Syntax of PHP heredoc

To create strings with PHP heredoc way you have to use <<< operator. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

<?php
  $x = <<<EOD
  your text goes here
EOD;

  echo $x;
?>
Browser Output
your text goes here

In this above example, EOD is an identifier. You can give any name to an identifier. See the following example –

<?php
  $x = <<<MY_IDENTIFIER
  your text goes here
MY_IDENTIFIER;

  echo $x;
?>

📢 Important Note –

The ending identifier, must not be indented, else it will not work and you will get an error. See the following examples –

📝 Don’t add space or spaces before the ending identifier.

<?php
  //Invalid Syntax
  $x = <<<MY_IDENTIFIER
  your text goes here
      MY_IDENTIFIER;

// Valid Syntax
  $x = <<<MY_IDENTIFIER
    your text goes here
MY_IDENTIFIER;
?>

Example of PHP heredoc

<?php
  $name = "John Doe";

  $x = <<<MY_IDENTIFIER
  Hi, My name is $name<br>
  An exampel text<br>
  'Text is inside the single quotes'<br>
  "Text is inside the double quotes"<br>
  <strong>Bold Text</strong>
MY_IDENTIFIER;

	echo $x;
?>
Browser Output
Hi, My name is John Doe
An exampel text
‘Text is inside the single quotes’
“Text is inside the double quotes”
Bold Text

PHP NowDoc

The PHP nowdoc(since PHP 5.3.0) is almost similar to the PHP heredoc. But they are not the same.

Syntax of PHP nowdoc

A nowdoc is identified with the same <<< sequence used for heredocs, but the identifier which follows is enclosed in single quotes.

<?php
  echo <<<'IDENTIFIER'
  your text goes here
IDENTIFIER;
?>

📝 All the rules for heredoc identifiers also apply to nowdoc identifiers, especially those regarding the appearance of the closing identifier.


Example of nowdoc

<?php
  $x = <<<'MY_IDENTIFIER'
  Hello World<br>
  'Text is inside the single quotes'<br>
  "Text is inside the double quotes"<br>
  <strong>Bold Text</strong>
MY_IDENTIFIER;

	echo $x;
?>
Browser Output
Hello World
‘Text is inside the single quotes’
“Text is inside the double quotes”
Bold Text

heredoc VS nowdoc

What are the Differences between heredoc and nowdoc?

The PHP herdoc is like a double-quoted string, and the PHP nowdoc is like a single-quoted string.

nowdoc can not parse the variable data but heredoc can –

<?php
$name = "John Doe";

$x = <<<HEREDOC
heredoc -  $name
HEREDOC;

$y = <<<'NOWDOC'
nowdoc - $name
NOWDOC;

echo $x.'<br>'.$y;
?>
Browser Output
heredoc – John Doe
nowdoc – $name

Escape sequences work with the heredoc, but escape sequences do not work with the nowdoc.

<?php
$x = <<<HEREDOC
<strong>heredoc Example -</strong>
escaping backslash \\ \nThis is new line \n\tA new line with tab Space
HEREDOC;

$y = <<<'NOWDOC'
<strong>nowdoc Example -</strong>
escaping backslash \\ \nThis is new line \n\tA new line with tab Space
NOWDOC;

echo "<pre>$x <br><br>$y </pre>";
?>
Browser Output
heredoc Example -
escaping backslash \ 
This is new line 
	A new line with tab Space 

nowdoc Example - escaping backslash \\ \nThis is new line \n\tA new line with tab Space