Hello everybody.. I've read about the "heredoc" way of outputting strings..
Where can we consider this use handy??
Is it used only to avoid escaping the single and double quotation mark??
Also, why is there an identifier to be put at the first and end of the function??
Thanks beforehand.

Recommended Answers

All 6 Replies

Heredoc syntax is a way to use custom identifier to delimit strings in PHP. Usualy string are delimited with single or double quotes, but with heredoc you declare (name) an identifier. This way you do not have to escape single and double quotes anymore. Te rules for chosing identifier name are same as for other labels in PHP. You put identifier in the beginning (after <<<) to let PHP know that now this is the delimiter for the string and you put at the end to let PHP know where the end of the string is. The line with closing identifier shall not contain any other characters (even no indent) which could be the only drawback since it might spoil the look of your code :-).

I use heredoc when I have alot of single and double quotes in text so I do not have to wory about escaping the ones that normaly should be escaped. A simple example of is when generating HTML code with PHP and the code consists of events that call Javascripts function with parameters. Using single or double quotes you would code like this:

$htmlButton = '<input type="button" onclick="someJsFunction(\'parameter1\',\'parameter2\',\'' . $var_parameter3 . '\')" />';
$htmlButton = "<input type=\"button\" onclick=\"someJsFunction('parameter1','parameter2','$var_parameter3')\" />';

Using heredoc syntax you can code this way (maybe a bit cleaner):

$htmlButton = <<< HTMLCODE
<input type="button" onclick="someJsFunction('parameter1', 'parameter2','$var_parameter3')" />
HTMLCODE;

Note variables within heredoc get parsed as with double quoted strings.

There is also nowdoc syntax which is similar but behaves as single quoted strings (ie the variables do not get parsed).

The link below might help to clarify more:

http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc

Thank you.. However, as I mentioned, I've read a lot about it, and I know what it is, but I wanted to know is it only handy when we want to avoid escaping the single and double quotes?? In addition, I want to know why is there an identifier?? I mean why isn't there a specific identifier to put always?? Why isn't this way used, for example, as the following always:

$MyVariable = <<< E
Here goes the value of the variable......
E;

without changing the letter "E" (or any other letter specified by PHP parser) whatever we are writing???

It leaves the choice to the coder, because you might want to use E; in your output. By leaving this choice to you, you can make sure it is something that is not used in your output.

By leaving this choice to you, you can make sure it is something that is not used in your output.

What do you mean by using it in my output??

Suppose PHP required it to be E;

$MyVariable = <<< E
I want to output E; on the start of the following line:
E;
E;

This would fail of course. Since you can choose what you want, you can change it to:

$MyVariable = <<< F
I want to output E; on the start of the following line:
E;
F;

Excuse me, I'm having a little headache today.. My mind is starting to hibernate.. ;)
:icon_idea: Maybe I have to get it restarted at some "Restartering's" :D.
Thank you so much, pritaeas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.