views:

730

answers:

4
+4  Q: 

PHP <<<EOB

I've been developing with PHP for some years now, and recently came across this code:

<?php
echo <<<EOB
    <html>
    <head>
        <title>My title</title>
    </head>
    ...
EOB;
?>

I've never seen this approach to print HTML, which seems to be pretty useful and less prone to some weird variable or double quote syntax error.

I've searched for some official information about this, and only found a post of Rasmus talking about this.

So my question is, can anyone explain me in details about this functionality and what EOB means? Maybe End Of Block?

Thanks in advance for all your answers.

+16  A: 

This is known as heredoc syntax. The documentation will tell you everything you need to know.

Essentially, however:

A third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.

The closing identifier must begin in the first column of the line. Also, the identifier 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.

So EOB is just what the author chose as his delimiter, not really sure what it stands for in his case but the identifier can be whatever you want.

Paolo Bergantino
I'm guessing "end of block" :).
ryanulit
Makes sense. :)
Paolo Bergantino
I've always wondered, is it pronounced "here - dock"?
Jonathan Sampson
Good question, I actually pronounce it "heary - dock" but thinking about that must be wrong :P
Paolo Bergantino
+2  A: 

It´s called heredoc and is described in the manual.

anddoutoi
+4  A: 

Just for the sake of completeness, heredoc in PHP is inherited from perl, which itself inherited it from the bourne shell.

wazoox
+1  A: 

official term is 'here document' i believe, usually shortened to 'heredoc'.

Brian