tags:

views:

215

answers:

6

I've never seen something like this before. So, it's confusing me for a while. But now I understand and use it sometimes. So, after brief experience, can anybody tell me what is the pro and cons using Heredoc Notation in your PHP?

$stringval = <<<MYHEREDOC

   just creating variable here. nothing more.

MYHEREDOC;

Personally, how do you use this PHP feature? Is it a bad way of coding or good way?

+3  A: 

There are now two types of "Heredoc:"

  • Heredoc, similar to quotation marks "" will convert any variables into their value on creation. This is very useful for putting exact spacing/returns where you need them (no need for \n or \t).
  • Nowdoc, similar to single marks '' will NOT convert any variables and stores just the text value. This can be used in default values of class variables (unlike heredoc).

Heredoc Example:

$value = 5;
$string = <<<EOL
The number is $value
EOL;
// The number is 5

Nowdoc Example:

$value = 5;
$string = <<<'EOL'
The number is $value
EOL;
// The number is $value

EDIT: The only downside I can think of is that the ending characters (in the examples, EOL) MUST be preceded by 0 spaces. This is hard to remember and looks ugly when inside nested functions:

function foo() {
  if ($bar) {
    $string = <<<LINE
Hey, how is it going, $name.
This is a great test of Heredoc and Nowdoc.
Blah blah blah, go upvote me!
LINE;
  }
}
St. John Johnson
@st. John Johnson, thanks you very much about the answer and the explanation. it's so detail. But about the checked one, i prefer @cletus way of coding. It's only a matter of preference.
justjoe
+2  A: 

Well, the pro(s) is

  • it's handy for large chunks of text with quotes

and the cons

  • you rarely if ever need large chunks of text in MVC
  • breaks indentation
  • has some quirks if used with classes (prior to 5.3)

Personally, i don't use it.

(an obligatory disclaimer about all this being a matter of preference, etc etc)

stereofrog
+2  A: 

I don't use it at all. It was quite useful in Perl, as Perl had no HTML escape feature. So, PHP does, and if I want to print out large amount of text, i'd just close PHP tag and type text as is. In any other case old good double quotes suits me well.

Col. Shrapnel
+3  A: 

99% of the time I use it, it's for SQL queries eg:

$sql = <<<END
SELECT *
FROM sometable
WHERE value > 100
ORDER BY name
END;

I find it easier to spot such queries in the source code and copy and paste them into something to run them. Your mileage may vary. Note: you can do multi-line with normal strings. I tend to avoid this however as the first line is indented differently to the rest.

The biggest "pro" as far as I'm concerned is that you don't need to escape quotes. That's particularly an issue with markup. Decide for yourself which is easier to read. Assuming:

$name = 'foo';
$type = 'text';
$value = 'Default value';

Version 1: single quotes

$html = '<input type="' . $type . ' name="' . $name . '" value="' . $value . '">';

Version 2: double quotes

$html = "<input type=\"$type\" name=\"$name\" value=\"$value\">";

Version 3: heredoc

$html = <<<END
<input type="$type" name="$name" value="$value">
END;

Note: in version 2 you can of course use single quotes for attribute values to solve the escaping problem but the point is you have to worry about things like this. Personally I don't like to mix attribute quote types in markup either.

cletus
In response to your note about version 2, you're right but it would become incorrect XHTML syntax. :D
St. John Johnson
XML allows single-quotes for attributes, therefore XHTML does also.
Jacob
+1  A: 

HEREDOCs are very useful for dumping large blobs of text into a variable, or directly out to the client. It also saves you multiple concatenations if you're building up a long multi-line string. The only main drawback is you need to pre-process any data you'll be inserting.

echo <<<EOF
<input type="text" name="foo" value="$bar" />

EOF;

is very readable, especially with an syntax-highlighting editor. But you do need to pre-process $bar with htmlspecialchars() OUTSIDE there heredoc (for escaping quotes), so you end up with:

$bar = htmlspecialchars($bar);
echo <<<EOF
etc....

Whereas if you're in "html mode", you could use:

<input type="text name="foo" value="<?php echo htmlspecialchars($bar) ?>" />

or

echo '<input type ... snip ... value="' . htmlspecialchars($bar) . etc....

Generally you don't need to store the escaped version of the data, so you might as well dump it out directly and save yourself the variable assignment step. HEREDOCs don't let you do that, as you can't insert a function call into them directly.

Marc B
+1  A: 
eagle