PDA

View Full Version : PHP: Why don't we use Heredoc more commonly?


FuRom
09-27-2007, 03:33 PM
Information about Heredoc (http://www.php.net/manual/en/language.types.string.php#language.types.string.sy ntax.heredoc)

Well, I was in the middle of some work for terapets, and I got bored and decided to learn something new about php. I was just reading some scripts I had not reviewed in my repository (I download and check out quite a few scripts daily, I got to the point I just archive and review to see if they're useful or hold usability in the future) and I noticed one had an echo that didn't use quotes. It was written like this:

echo <<<END_DELIMETER
Your content goes here. You can use $var or $var[blah] and such... you can also write " or ' without having errors...
END_DELIMETER;

I know it's not pretty because you can't whitespace the end delimeter, but this is useful. I got further into using it and found you can define variables using it too! I didn't know what to call this technique, so I asked a php veteran, OwlManAtt, and he linked me to that php.net documentation about it.

So, why do we not use something that is so useful? I know it doesn't look pretty, but it is good for a lot of things. Did you even know about this untill now?

Patrick
09-27-2007, 03:39 PM
I use it sometimes. I guess its just habit. I'm more used to using echo so I don't use heredoc all that often. Sometimes though.

Jake
09-27-2007, 04:09 PM
i use it, its a very good habbit to get into in my mind for template peices.

Sagashiteru
09-27-2007, 04:27 PM
I use it too,
could be because I used to mod IPB XD
it always comes in handy, less typing if you want to echo correct html XD

FuRom
09-27-2007, 07:31 PM
it always comes in handy, less typing if you want to echo correct html XD

lol, I've always used the ignore delimiter \ on my quotes to do proper HTML. Like so:

echo "I have HTML <img src=\"blah.gif\">";
echo 'I\'ve been a naughty programmer =(';

Jake said something about it being good to keep in mind with templates. I find it's has massively great applications when having a template system in your site.

fantasia
09-27-2007, 08:09 PM
:O I'd never even heard of it, but i love learning new stuff when it comes to code, so thanks for pointing it out :D i'll be looking into it! :)

jlp09550
09-27-2007, 08:27 PM
I use it a lot.. it just makes it easier to code certain things.. and much more organizable.

Tigress
09-27-2007, 08:38 PM
Because for the most part you can use a proper templating engine like Smarty instead?

OwlManAtt
09-27-2007, 08:56 PM
Heredoc really doesn't add a lot of value to PHP.

I will remind you that PHP itself is just a glorified templating engine.


<html>
<head>
<title>Foo!</title>
</head>
<body>
<h1><?php print $_REQUEST['page_title']; ?></h1>
<p>Hello there, <?php print $_COOKIE['name']; ?>!</p>
</body>
</html>


The essential question is: Why do we need Heredoc for HTML if we can just instruct the server to stop evaluating PHP and then start it back up when we're doing with our block of HTML?

FuRom
09-27-2007, 09:19 PM
The essential question is: Why do we need Heredoc for HTML if we can just instruct the server to stop evaluating PHP and then start it back up when we're doing with our block of HTML?

Good question! I'll take on the challenge of answering it! The answer isn't specifically stating to "use heredoc" though. When you're designing an application to be used by the average user that just wants to start a community, like a message board system or CMS, you have to build in a good template system that is easy for the user to mod and edit. That's not to mention making a comprehensive scripting system.

Vivacity
09-30-2007, 12:59 AM
Using heredoc with echo is the slowest way to parse data to a server, the way I learned it. That's what it's not used that commonly, it takes more time.

echo ''; is quickest
echo ""; is second
echo <<< END_DELIMETER str
END_DELIMETER; is the slowest.

Reason being is single quotations don't search for variables, making them the quickest, double quotations search for variables, making them the second quickest, heredoc, weeds through and searches for variables, while searching for the end_delimeter; line, making it slightly slower than echo "";. However, if you aren't experiencing large ammounts of traffic, you will hardly be able to tell the difference in load times between the three.

shadowpwner
10-09-2007, 05:34 PM
Interesting, I've never seen that command before.

Then again, as a PHP newbie, I only now about 20 commands.