构造SQL语句时,插值(使用{}弯曲括号)和串联(.或点)之间的区别 [英] Difference between interpolation (using {} squiggly brackets) and concatenation (., or dot) when constructing an SQL statement
本文介绍了构造SQL语句时,插值(使用{}弯曲括号)和串联(.或点)之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有什么区别:
$sql = "select * from {$table}";
和这个:
$sql = "select * from ".$table;
有什么区别吗?
推荐答案
除了使用串联和不使用串联外,没有什么区别.
There isn't much of a difference other than one uses concatenation and one doesn't.
您也可以将其写为
$sql = "select * from $table";
您也可以在字符串中使用{}
来引用对象.
You can use {}
in your string to refer to objects as well.
如果表是名称或数组
$sql = "select * from {$table->name}"; //works
$sql = "select * from $table->name"; //works too
$sql = "select * from $table->innerTable->table"; //doesn't work
$sql = "select * from {$table['name']}"; //works
$sql = "select * from $table['name']"; //breaks
我个人使用它来提高可读性,因为我总是知道我在指的是变量.
I personally use it to increase readability, because I'll always know I'm referring to a variable.
这篇关于构造SQL语句时,插值(使用{}弯曲括号)和串联(.或点)之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文