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,785
  • 15
  • 68
  • 103

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
  • 11,056
  • 4
  • 42
  • 71
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
  • 87,921
  • 11
  • 154
  • 174
jeroen
  • 91,079
  • 21
  • 114
  • 132
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
  • 20,196
  • 13
  • 80
  • 123