Heredoc

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Heredoc

Post by uberpolak »

Okay, a great deal of people in this forum have mentioned heredoc frome time to time, and some examples of using it have come up. I've done some reading about it, and I just have one question - what's so damn great about it?
From what I gather, it's an alternate method of echoing text, but there must be more to it if it's so popular. So, heredoc aficionados, what aren't I getting about this?
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

Ahh yes, Mark is snoring away right now so I'll take my time to explain. Nevermind that part.

Anyhow, with heredoc, you won't need to escape characters, add \n for line breaks for variables, returning or printing. Usage:

Code: Select all

$var = <<< END
this
is
my
name
END;

// is equal to:

$var = "this\nis\nmy\nname";

print <<< RESULT
<table cellpadding="0" cellspacing="0" border="0">
   <tr>
      <td>Name</td>
      <td>Age</td>
   </tr>
   <tr>
      <td>{$rows['name']}</td>
      <td>{$rows['age']}</td>
   </tr>
</table>
RESULT;
With heredoc, you can also indent the code as show above. You can also use it wit return() for returning things from functions. Anyhow, at the end of the day, it's still the whole personal preference thing. I code VB-style, so I know which ends what with heredoc so I don't get parse errors everywhere.

-Nay
User avatar
uberpolak
Forum Contributor
Posts: 261
Joined: Thu Jan 02, 2003 10:37 am
Location: Next to the bar

Post by uberpolak »

Actually, that seems quite useful. The ability to indent code alone makes it seem worth looking into for me, thanks.
Post Reply