What does "<<<" do in PHP?

 if(1+1=2){
<<<HTML

// Whatever

<<<END

}elseif(2+2=4){<<<

// I've seen is used on its own. ex:
<<<END
}else{<<<MYSQL QUERY
// or with other text beside HTML
<<<END
}
/* I DONT understand what it does or what its used for.... at complete loss and I've devloped for years in php. lol.*/

I have no idea how this operator or whatever its called works I’ve search PHP.net / Google already but if I could just get a laymen-terms explanation. That would be great thnx.

<<< is used in what is called a “here document”
It is a convenient way to place a long string of text into a variable.
The symbol END is arbitrary. I often call it ENDO (an old boat builder’s term).
Be sure to have no whitespace to the right of END or END; in any
copy of or elaboration on the codes below.

function startHtml()
{
$self = basename($_SERVER[‘PHP_SELF’]);

$ret = <<< END
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd”>
<html>
<head>
<title> $self </title>
<meta http-equiv=“Content-Type” content=“text/html;charset=utf-8”>
</head>
<body>
END;

return $ret;
}

…a real-world startHTML($cssfiles) might take an incoming parameter, that could be an array of CSS files to link into the head. Here docs are handy.

Basically tells PHP to count everything following as a string until the end point.


print <<<END
<html>
   <head></head>
</html>
END;


END can be any word you want but most people just use END as its clear what it is.

Thanks guys I’ve been studying PHP for over 5 years and never came across an explanation is this depreciated or just not common what are the flaws it does anyone know how it treats variables is it like double quote where vars are interpreted or like single?

smpily, see php.net/<<< (scroll down to “heredoc”).

No its not deprecated (yes thats spelt correct - we have our own special word in programming for old stuff we no longer want). It’s a very common technique. Once you’ve been through a few hundred scripts looking for code to solve particular problems you’ll see it in quite a few of them. It is an older technique yes but still one that is common. Today most people just seperate their php code and html code into seperate files and then use some sort of template system which basically does the same job. Using heredoc simply means you can do it in the same file.