-1

Possible Duplicates:
How do I use that “<<<HTML” thing in PHP?
PHP <<<EOB

   $sql = <<<MySQL_QUERY
    CREATE TABLE IF NOT EXISTS testDB (
    title VARCHAR(150),
    bodytext  TEXT,
    created VARCHAR(100)
  )
  MySQL_QUERY;

I've seen people use the above to include multiple lines of string and set it to a variable, was wondering if there is a name for this kind of use? So that I can look it up.

Community
  • 1
  • 1
chutsu
  • 13,612
  • 19
  • 65
  • 86
  • 1
    Dupe at least a few times: http://stackoverflow.com/questions/1048481/php-eob http://stackoverflow.com/questions/571894/how-do-i-write-the-toc-thing-in-php http://stackoverflow.com/questions/1040578/how-do-i-write-this-out-in-php-where-i-would-use-the-to-print-out-content http://stackoverflow.com/questions/1203479/how-do-i-use-that-html-thing-in-php – karim79 Aug 20 '09 at 12:03
  • 2
    It is a dupe, but dupes are hard to find if you don't know to search for 'heredoc'. – Tom Haigh Aug 20 '09 at 12:18
  • @Tom Haigh - good point, do you think questions like 'what the heck is this syntax' should be exceptions to the dupe rule? – karim79 Aug 20 '09 at 12:22
  • @karim79 maybe, or maybe there could be a search that would let you search for a set of characters like that. – Tom Haigh Aug 20 '09 at 12:32

1 Answers1

10

Heredoc syntax:

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.

Warning It is very important to note that the line with the closing identifier must contain no other characters, except a semicolon (;). That means especially that the identifier may not be indented, and there may not be any spaces or tabs before or after the semicolon. It's also important to realize that the first character before the closing identifier must be a newline as defined by the local operating system. This is \n on UNIX systems, including Mac OS X. The closing delimiter must also be followed by a newline.

If this rule is broken and the closing identifier is not "clean", it will not be considered a closing identifier, and PHP will continue looking for one. If a proper closing identifier is not found before the end of the current file, a parse error will result at the last line.

Heredocs can not be used for initializing class properties. Since PHP 5.3, this limitation is valid only for heredocs containing variables...

gnat
  • 6,213
  • 108
  • 53
  • 73
Paolo Bergantino
  • 480,997
  • 81
  • 517
  • 436