Difference between interpolation (using {} squiggly brackets) and concatenation (., or dot) when constructing an SQL statement
What´s t开发者_Python百科he difference between:
$sql = "select * from {$table}";
and this:
$sql = "select * from ".$table;
Are there any differences?
The outcome will be the same, but of course you do two fundamental different things:
The first one is variable parsing (which only works in double quoted and heredoc strings), the second one is string concatenation.
There isn't much of a difference other than one uses concatenation and one doesn't.
You can also write it as
$sql = "select * from $table";
You can use {}
in your string to refer to objects as well.
If table were a name or array
$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.
No - both will evaluate within PHP to the same string.
So will
$sql = "select * from $table";
you even can do this in php: $sql = "SELECT * FROM $table ";
it's just an alias.
As mentioned both statements result in the same string being assigned to $sql. Note though that using single quotes and concatenation is slightly more efficient since the string do not need to be parsed by the PHP interpreter:
$sql = 'select * from ' . $table;
精彩评论