I read that heredoc is for long strings of text specifically multi lines. So are we talking quantity of text or formatting?
One example I read is that someone used to use heredoc for writing out sql strings in php, so they could read the query easier.
But I write queries all the time on multiple lines without a problem, without any form of concatenating:
$sql = "SELECT
I just don't understand if heredoc is for, say, a paragraph of text, or multiple lines ( formatting wise ), that need to echo out on the page in multiple lines.
Using heredoc eliminates the need for the escaping of special characters, unlike with strings. So, if you have a large block of text or code that you want displayed somewhere without going through the trouble of escaping everything, you have that option.
heredocs is really great for writing out large blocks of html code, mixed in with some php vars. No php start/end tags, no writing echo statements with quotes and semis. Just start the heredocs block, write your html with perhaps a php var in the middle to display some dynamic content and then close the heredocs and echo that variable (that's how I do it). So much easier and much more readable!
It's a good candidate for when you have a string with many quotes. You shouldn't have to escape anything but $
$str = <<<EndMe
On the seat back of the bench on which Shelly O'riley sat was the plaque inscribed "In Loving Memory"
but she neither felt loved nor wished to have any memory of the previous forty-eight hours.
"M'am. Excuse me! M'am?" the young undertaker's assistant came calling down the lane.
Though I suppose that example could really use Nowdoc since there aren't any variables used in it.
This topic is now closed. New replies are no longer allowed.