What is the advantage of using Heredoc in PHP, and can you show an example?
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
Some IDEs hightlight the code in heredoc strings automatically - which makes using heredoc for xml or html visually appealing.
I personally like it for longer parts of i.e. XML since I don't have to care about quoting quote characters and can simply paste the XML.
First of all, all reasons are subjective.
It's more like a matter of taste rather than a reason.
Personally, I find heredoc quite useless, and use in occasionally, most of time when I need to get some HTML into a variable and don't want to bother with output buffering, to form an HTML email message for example.
Formatting doesn't fit general indentation rules, but I don't think it's a big deal.
As for the examples in the accepted answer, it is merely a cheating.
String examples in fact being more concise if one wont cheat on them
Heredoc's are a great alternative to quoted strings because of increased readability and maintainability. You don't have to escape quotes and (good) IDE's or text editors will use the proper syntax highlighting.
A VERY common example: echoing out HTML from within PHP:
Easy to read. Easy to maintain.
The alternative is echoing quoted strings, which end up containing escaped quotes and IDEs aren't going to highlight the syntax for that language, which leads to poor readability and more difficulty in maintenance.
Updated answer for Your Common Sense
Of course you wouldn't want to see an SQL query highlighted as HTML. To use other languages, simply change the language in the syntax:
I don't know if I would say heredoc is laziness, one can say that doing anything is laziness, as there are always more cumbersome ways to do anything.
For example, in certain situations you may want to output text, with embedded variables without having to fetch from a file and run a template replace. Heredoc allows you to forgo having to escape quotes, so the text you see is the text you output. Clearly there are some negatives, for example, you can't indent your heredoc, and that can get frustrating in certain situation, especially if your a stickler for unified syntax, which I am.
The here doc 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:
To me this has a lower probability of introducing a syntax error than using quotes:
Another point is to avoid escaping double quotes in your string:
Problem with the above is the syntax error (the missing escaped quote) I just introduced as opposed to here document syntax:
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"