What is the difference Heredoc and Nowdoc in php ?

Heredoc  :

Heredoc string function act just like quoted strings but it starts with <<< followed by an identifier and begins with the new line ,then the set of strings and finally ends with the same identifier and finally semi semicolon .

Example : 
<?php
$name = “example”;
$str = <<<EOD
This is an heredoc $name 
with php interview questions 
Answers.
EOD;

echo $str;

?>
Output : 
This is an heredoc example with php interview questions Answers.

Note : Here from above example ,  EOD is an an identifier , and there should not be whitespace located at end of a line, without any other characters following it.

Nowdoc : 

Nowdoc is similar to heredoc , variables are not accepted in Nowdoc just like single quote , whwrre as heredoc accepts the variables .

Example : 
<?php
$name = “example”;
$str = <<<EOD
This is an nowdoc $name 
with php interview questions 
Answers.
EOD;

echo $str;

?>

Output : 
This is an nowdoc $name with php interview questions Answers.

To know about  single quote and double quote please click this link

Leave a Reply

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