What does '<<<END ... END' do?
I was looking through some scripts over the weekend and came across a structure that I'm not familiar with...
I'm guessing it allows one to assign HTML to a variable, including quotes, without the need to use the backslash. Is that correct?
$variable = <<<END
<a href="http://www.something.tld">Some HTML here</a>
If so, then are there any restrictions or pitfalls?
If not, then what does it do?
Can anyone point me to a reference page for the structure?
From the manual:
Another way to delimit strings is by using heredoc syntax ("<<<"). One should provide an identifier after <<<, then the string, and then the same identifier to close the quotation.
The closing identifier must begin in the first column of the line. Also, the identifier used must follow the same naming rules as any other label in PHP: it must contain only alphanumeric characters and underscores, and must start with a non-digit character or underscore.
It is very important to note that the line with the closing identifier contains no other characters, except possibly a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs after or before the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by your operating system. This is \r on Macintosh for example.
If this rule is broken and the closing identifier is not "clean" then it's not considered to be a closing identifier and PHP will continue looking for one. If in this case a proper closing identifier is not found then a parse error will result with the line number being at the end of the script.
Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.
Thanks so much.
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)