Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between interpolation (using {} squiggly brackets) and concatenation (., or dot) when constructing an SQL statement

Tags:

sql

php

mysql

What´s the difference between:

$sql = "select * from {$table}";

and this:

$sql = "select * from ".$table; 

Are there any differences?

like image 266
alxsimo Avatar asked Apr 07 '26 17:04

alxsimo


2 Answers

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.

like image 157
Felix Kling Avatar answered Apr 09 '26 06:04

Felix Kling


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.

like image 22
JohnP Avatar answered Apr 09 '26 06:04

JohnP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!