Here documents
Archive - Originally posted on "The Horse's Mouth" - 2004-08-28 16:00:26 - Graham EllisIf you find yourself repeating something within a piece of code .... there's probably an easier way. After all, most programmer are lazy and that applies just as much (if not more) to the authors of the languages. Remember to use functions / methods / subs / procs / defs rather than cut and paste, and remember to use required / used / included / imported files too.
From time to time, I see a chunk of text with the word "print" carefully inserted in front of each line, and each line of text embedded in quotes. This is just the repetition I'm suggesting that you avoid - although in this case the solution is a "here document".
In Perl, write:
print <<"DONE";
This is a block of text to print
that is several lines long
DONE
and in PHP:
print <<<DONE
This is a block of text to print
that is several lines long
DONE;
References: [Perl] and [PHP].
Samples [Perl] and [PHP].