Jump to content

HEREDOC Syntax.


sepoto

Recommended Posts

Do I need to do anything to enable HEREDOC? I have some existing code that is using HEREDOC Syntax and I want to check to see if my PHP server is supporting it. I am familiar with using phpinfo();, I just don't know what to look for yet.Thanks

Link to comment
Share on other sites

Do I need to do anything to enable HEREDOC? I have some existing code that is using HEREDOC Syntax and I want to check to see if my PHP server is supporting it. I am familiar with using phpinfo();, I just don't know what to look for yet.Thanks
As far as I know, HEREDOC doesn't have to be enabled, and has been supported by php for a long time.
$a = <<<ENDthis string can go on forever and contain almost anything "$';<>@$)*^&!)_*$END;

That is the purpose of a HEREDOC, you could literally copy and paste just about any text into a heredoc and you then have it in string form.edit: After, brushing up on my HEREDOC understanding, apparently HEREDOC does behave like double quotes and interpolate variables and character escape codes. My mistake.

Link to comment
Share on other sites

A trick to heredocs that many people miss is that the closing delimiter (the word END in Dilated's example) MUST be typed immediately after the linebreak. No tabs, spaces, or other characters are permitted. It's counterintuitive because we have all been taught to indent code for readability. Heredoc syntax is an exception.

Link to comment
Share on other sites

Anything.Edit. from the manual:

the identifier must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
Link to comment
Share on other sites

No tabs, spaces, or other characters are permitted. It's counter-intuitive because we have all been taught to indent code for readability. Heredoc syntax is an exception.
If you have a problem with HEREDOCs, this is most likely the problem. It was a major point of confusion when I became familiar with HEREDOCs.
edit: After, brushing up on my HEREDOC understanding, apparently HEREDOC does behave like double quotes and interpolate variables and character escape codes. My mistake.
PHP 5.3.0 also supports NOWDOC, which behaves like single-quotes. The difference is that you enclose the opening delimiter in single quotes.
<<<'NOW'  This is a Nowdoc.   It behaves like a single quote.  It will print $everything literally. NOW;

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...