What is the advantage of using heredoc in PHP, and can you show an example?
The heredoc syntax is much cleaner to me and it is really useful for multi-line strings and avoiding quoting issues. Back in the day I used to use them to construct SQL queries:
$sql = <<<SQL
select *
from $tablename
where id in [$order_ids_list]
and product_name = "widgets"
SQL;
To me this has a lower probability of introducing a syntax error than using quotes:
$sql = "
select *
from $tablename
where id in [$order_ids_list]
and product_name = \"widgets\"
";
Another point is to avoid escaping double quotes in your string:
$x = "The point of the \"argument" was to illustrate the use of here documents";
The pProblem with the above is the syntax error (the missing escaped quote) I just introduced as opposed to here document syntax:
$x = <<<EOF
The point of the "argument" was to illustrate the use of here documents
EOF;
It is a bit of style, but I use the following as rules for single, double and here documents for defining strings:
'no variables here'
"Today is ${user}'s birthday"