[ACCEPTED]-What is the advantage of using heredoc in PHP?-heredoc

Accepted answer
Score: 229

The heredoc syntax is much cleaner to me 13 and it is really useful for multi-line strings 12 and avoiding quoting issues. Back in the 11 day I used to use them to construct SQL 10 queries:

$sql = <<<SQL
select *
  from $tablename
 where id in [$order_ids_list]
   and product_name = "widgets"
SQL;

To me this has a lower probability 9 of introducing a syntax error than using 8 quotes:

$sql = "
select *
  from $tablename
 where id in [$order_ids_list]
   and product_name = \"widgets\"
";

Another point is to avoid escaping 7 double quotes in your string:

$x = "The point of the \"argument" was to illustrate the use of here documents";

The problem 6 with the above is the syntax error (the 5 missing escaped quote) I just introduced 4 as opposed to here document syntax:

$x = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;

It is 3 a bit of style, but I use the following 2 as rules for single, double and here documents 1 for defining strings:

  • Single quotes are used when the string is a constant like 'no variables here'
  • Double quotes when I can put the string on a single line and require variable interpolation or an embedded single quote "Today is ${user}'s birthday"
  • Here documents for multi-line strings that require formatting and variable interpolation.
Score: 74

Heredoc's are a great alternative to quoted 14 strings because of increased readability 13 and maintainability. You don't have to escape 12 quotes and (good) IDEs or text editors will 11 use the proper syntax highlighting.

A very common 10 example: echoing out HTML from within PHP:

$html = <<<HTML
  <div class='something'>
    <ul class='mylist'>
      <li>$something</li>
      <li>$whatever</li>
      <li>$testing123</li>
    </ul>
  </div>
HTML;

// Sometime later
echo $html;

It 9 is easy to read and easy to maintain.

The 8 alternative is echoing quoted strings, which 7 end up containing escaped quotes and IDEs 6 aren't going to highlight the syntax for 5 that language, which leads to poor readability 4 and more difficulty in maintenance.

Updated answer for Your Common Sense

Of course 3 you wouldn't want to see an SQL query highlighted 2 as HTML. To use other languages, simply 1 change the language in the syntax:

$sql = <<<SQL
       SELECT * FROM table
SQL;
Score: 8

Some IDEs highlight the code in heredoc 6 strings automatically - which makes using 5 heredoc for XML or HTML visually appealing.

I 4 personally like it for longer parts of i.e. XML 3 since I don't have to care about quoting 2 quote characters and can simply paste the 1 XML.

Score: 5

First of all, all the reasons are subjective. It's 12 more like a matter of taste rather than 11 a reason.

Personally, I find heredoc quite 10 useless and use it occasionally, most of 9 the time when I need to get some HTML into 8 a variable and don't want to bother with 7 output buffering, to form an HTML email 6 message for example.

Formatting doesn't fit 5 general indentation rules, but I don't think 4 it's a big deal.

       //some code at it's proper level
       $this->body = <<<HERE
heredoc text sticks to the left border
but it seems OK to me.
HERE;
       $this->title = "Feedback";
       //and so on

As for the examples in 3 the accepted answer, it is merely cheating.
String 2 examples, in fact, being more concise if 1 one won't cheat on them

$sql = "SELECT * FROM $tablename
        WHERE id in [$order_ids_list]
        AND product_name = 'widgets'";

$x = 'The point of the "argument" was to illustrate the use of here documents';
Score: 4

I don't know if I would say heredoc is laziness. One 13 can say that doing anything is laziness, as 12 there are always more cumbersome ways to 11 do anything.

For example, in certain situations 10 you may want to output text, with embedded 9 variables without having to fetch from a 8 file and run a template replace. Heredoc 7 allows you to forgo having to escape quotes, so 6 the text you see is the text you output. Clearly 5 there are some negatives, for example, you 4 can't indent your heredoc, and that can 3 get frustrating in certain situation, especially 2 if your a stickler for unified syntax, which 1 I am.

More Related questions