Question

I just happened to see the following way to compose a parameterized SQL query:

function select_user($uid)
{
    // what is '<<<'?  
    // I can't google any document about it
    // (or I don't know how to search symbol)

    $sqlStr = <<< SQL_STR

         SELECT * FROM user WHERE uid = ?

SQL_STR; // must put in the begin of the line
         // and it must match the word at the right hand side of '= <<<'

    // Code Igniter Database Class
    return $this->db->query($sqlStr, array($uid));
}

To rephrase my questions in here:

  • What the symbol '<<<' does?
  • My colleague said that 'SQL_STR' must be matched, why?
Was it helpful?

Solution 2

What your looking for is called heredoc.

For what it's worth, the SQL query is irrelevant to the string assignment:

$html = <<<HTML
    Imagine some HTML here with interspersed $variables
HTML;

It's of course not limited to HTML either. It has quite a few useful properties for large blocks of text. Namely, you can interpolate variables into it in a pleasant manner, and you don't have to escape single or double quotes. (According to the manual: "Heredoc text behaves just like a double-quoted string, without the double quotes.")

OTHER TIPS

A Heredoc is used to define a string, usually because of the benefit of not having to escape quotes throughout the string, unlike a string literal.

From the Manual:

Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped, but the escape codes listed above can still be used. Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.

$str = <<<EOD
Example of string
spanning multiple lines
using heredoc syntax.
EOD;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top