Jump to content
MakeWebGames

heredoc, heredoc what?


Floydian

Recommended Posts

This is just a little something I rarely ever see in any scripts. I've seen quite a few scripts from some of the coders floating around and many many more scripts on the net, and I don't think I've seen heredoc used once :?

here doc looks like this:


echo <<<EOT
The three < followed by three capital letters of your choice are a substitute for quotes.
EOT;

 

Some ground rules for using heredoc are as follows: on the first line, immediately following the <<<EOT, there can be NO spaces.

You must start a new line afterwards.

The ending EOT; must not have any spaces before it, or after it. I must be on a line all of it's own. Don't tab it in! lol

Benefits of using heredoc:

Heredoc consistently is faster than using the double quote ( " ) but is slower than ( ' ). So when you need to put variables inside of quotes, heredoc is actually a faster syntax.

Heredoc is especially good for doing something like:

 


echo <<<EOT
<table width="100%">
  <tr>
       <td>
       I like using contractions in my sentences because not using them doesn't seem to feel right somehow.
       </td>
   </tr>
</table>
EOT;

 

Note how the use of both kinds of quotes does not require any escaping. This is way more valuable for javascript code.

 


[url="javascript:self.a_js_function('blah', 'blah2')"]Click here to execute js function.[/url]

 

Doing that without heredoc would requre some nasty escaping....

Drawbacks: You can not concatenate, so if you want to use functions on something that is getting echoed, do the function first, then store it in a variable you can later echo in the echo.

I don't consider this a drawback though, as seperating PHP code from HTML presentation is very good form and should be done be everyone.

There you go, the wonders of heredoc explained. Hope it helps ya.

Link to comment
Share on other sites

Guest Anonymous

Re: heredoc, heredoc what?

I use these quite alot to save time also say is you use thse quite alot ....

PRINT<<<NAME

html here.

NAME;

you can rename NAME to any thing to make it alot easyer to develop the page & not getting mixed up.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

Using start and end tags works out just fine. However, if you have a lot of them, your script will execute slower than otherwise.

Personally, I found that adding in lots of those tags makes the html harder to read. That's just my own personal opinion on it though.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

 

Using start and end tags works out just fine. However, if you have a lot of them, your script will execute slower than otherwise.

Personally, I found that adding in lots of those tags makes the html harder to read. That's just my own personal opinion on it though.

Actually, it doesn't effect speed; Unless the server has a 100mhtz processor.

Most php/script editors grays html when you close the php script, zend even hides it.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

 

 

What you missed in that deal Lost, is the use of both ' and " which means one of them would have to escaped without the use of heredoc.

The inner quotes in javascript are supposed to be ' and the outer ones, the ones that belong the href html part, are supposed to be "

 

echo "[url="javascript:self.a_js_function('blah', 'blah2')"]Click here to execute js function.[/url]";

 

that would be the equivalent of

 

echo <<<EOT
[url="javascript:self.a_js_function('blah', 'blah2')"]Click here to execute js function.[/url]
EOT;

 

It's a personal preference, but html that doesn't have any escaping, and isn't concatenated at all, is much easier to read, and therefore much easier to fine tune, than html that is escaped and concatenated.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

I've seen just about all the code on coveofpirates, I've seen quite a bit of mccodes vs1 code, a little bit of mccodes vs2, I've read hundreds of tutorials online, and never came across heredoc quotes.

But alas, this isn't a debate, just a lil something that perhaps someone reading it will not have known, and will appreciate the cleanliness afforded by heredoc.

And mccodes code is horribly written my friend. Just about every rule of good clean code was broken.

Not the least of which is defining functions before you call them. Just look at the admin file, there's what, 40 or so functions in a switch at the top, with the functions defined after it. That switch should be after the function definitions.

Anyways, I posted this lil snippet because I know I've seen some folks around this board that are beginner coders and they might like to see a some words of wisdom (not that they are, just couldn't come up with a better name... lol)

I'll post some more at some point. Prolly when I'm board.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

Yes I know how bad the programming is. I'm a programmer by profession.

I was just referencing that the base code of McCodes, which most of the wanna-be programmers on here have dealt with, uses this already so they should be familiar with it.

Link to comment
Share on other sites

Re: heredoc, heredoc what?

I took a look, and the heredoc is in the header, but only in the part that defines the html head.

I doubt that's getting edited a lot. lol

99.9999% of it is print....

so your point? By the way, hopefully you saw the post where I was pointing out the problems in your superstreets mods.

Apparently, half of the the entire thing could not contain anything in the coordinates lol. I'm sure that was a simple oversight....

But yeah, I fixed it and it works properly now :p

Link to comment
Share on other sites

Re: heredoc, heredoc what?

I do not run my super streets mod. I'm not a fan of the concept as it does not relate to any of my games. It was written by request of another CE user. So unless someone points out a problem and asks me to fix it, I have no idea if there are any issues when you get into full-scale use. And no I have not seen your post and no such problems were reported directly to me.

Edit: The mod was not designed to have coordinates. The mod is a clone of what you find at Hobowars.com If you wish to change the design of the mod to use such coordinates, you are correct it would need modification.

UCC

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...