PHP HereDoc, NowDoc, and HereDoc VS NowDoc

PHP HereDoc, NowDoc, and HereDoc VS NowDoc

In this chapter, you will learn – PHP heredoc, PHP nowdoc, and what is the difference between the heredoc and 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 need 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 below example

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

echo $x;
?>

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

Wrong or Invalid

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

echo $x;
?>

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

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

echo $x;
?>

An Example of heredoc

In the below example we can see that –
we can do variable interpolation, we can use HTML tags inside that, and we also use single and double quotes together without using escape sequences.

<?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 similar.

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;
?>

And 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

Difference between heredoc and nowdoc –

The PHP herdoc is like double-quoted string, and the PHP nowdoc is like 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

Previous is – PHP String, Escape Sequences and Concatenation

Posts Navigation


Leave a Reply

Your email address will not be published. Required fields are marked *