我刚好看到下面的构造参数化SQL查询的方式:
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));
}
在这里重新表述我的问题:
- 什么符号'
<<<
' 是吗? - 我的同事说'
SQL_STR
' 必须匹配,为什么?
最佳答案
Heredoc 用于定义字符串,通常是因为不必在整个字符串中转义引号,这与字符串文字不同。
来自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;
关于php参数化SQL查询特殊运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13492464/