...

View Full Version : What on earth is the "<<<" notation?



XmisterIS
04-27-2012, 09:47 AM
I've been coding PHP for a while and just now I've come across some new string notation here: http://php.net/manual/en/domdocument.loadxml.php

For example:



$str = <<<XML
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE root [
<!ENTITY nbsp "*">
]>
<div>This&nbsp;is a non-breaking space.</div>
XML;


What's with the <<<XML .... XML; notation? How is it different, functionally, to quoting the markup in a string?

EDIT: Ha ha ha! I've just tried using that notation in some code and it just gives me a syntax error! Works fine with quotes though.

abduraooft
04-27-2012, 10:03 AM
It's the heredoc (http://www.php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc)

tangoforce
04-27-2012, 12:57 PM
<<< is a heredoc. It's very useful as it allows you to return a whole load of stuff into a string with variables included in it. In the sample code you provided, everything upto the XML; will be stored in the variable called $str.

There is also the newer nowdoc which works like this:

$str<<<'XML'

//stuff

XML;

The nowdoc will return anything literally as you see it. If you have any variables in there they will be returned as variables including the $ sign and NOT replaced by their values.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum