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?
99% of the time I use it, it’s for SQL queries eg:
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:
Version 1: single quotes
Version 2: double quotes
Version 3: heredoc
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.