3

Possible Duplicate:
What is the name for the “<<<” operator?

I've just seen this code on this website:

PHP for Beginners: Building Your First Simple CMS

The code is:

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

    return mysql_query($sql);
}

I've search high and low for this operand "<<<" but I can't seem to find the documentation anywhere!

If this operand is real I would like to know how it works because writing SQL queries like this makes code much more readable and would make SQL bugs simpler to find!

Might i add that i tried this code and got a compile error:

Parse error: syntax error, unexpected $end in C:\Users\Alex Morley-Finch\Dropbox\Shared\Projects\htdocs\test\php\database.php on line 121 
Community
  • 1
  • 1
AlexMorley-Finch
  • 6,618
  • 15
  • 67
  • 101

3 Answers3

5

In the heredoc syntax, the last line (the one with the closing identifier) can't be indented, you have to do this:

<?php
       private function buildDB() {
$sql = <<<MySQL_QUERY
 CREATE TABLE IF NOT EXISTS testDB (
     title       VARCHAR(150),
     bodytext    TEXT,
     created     VARCHAR(100)
)
MySQL_QUERY;

              return mysql_query($sql);
       }
codeling
  • 10,672
  • 4
  • 37
  • 67
3

It's called a heredoc and you need to make sure that the ending line starts at the beginning of the line, so no:

    MySQL_QUERY;

but:

MySQL_QUERY;
DaveRandom
  • 86,228
  • 11
  • 149
  • 173
jeroen
  • 90,003
  • 21
  • 112
  • 129
1

<<< is a HEREDOC. You can read about it here. The most important thing to remember when using it is that the last line must not have any spaces.

dnagirl
  • 19,786
  • 13
  • 77
  • 119