Sometimes we need to write up long strings in PHP and things might get confusing the next time we read through it or someone else takes a look at the code. That’s when heredoc comes to the rescue.
The way to use heredoc is as follows
$var_name = <<<HEREDOC_NAME
/*
String assignment values go here
*/
HEREDOC_NAME;
<<<HEREDOC_NAME marks the beginning and the HEREDOC_NAME; marks the end. A heredoc name can be anything you please, but the same heredoc name should be used at both the beginning and the end of the string. A very important note – No characters, whitespace, indentation should be present either before or after the end heredoc statement,meaning that there should be nothing before or after HEREDOC_NAME; nothing includes whitespace/indentation too!
Here’s an example using heredoc:
<?php
$data = <<<END //start of heredoc
<html>
<head></head>
<body>
<ul>
<li> Point1 </li>
<li> Point 2 </li>
</ul>
</body>
</html>
END;
//end of heredoc, no whitespace/indentation/characters before or after the end heredoc statement
echo $data;
?>
Download the official 2buntu app for both Android and Ubuntu Touch.