Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference an associative array from inside a string?

"CREATE TABLE IF NOT EXISTS $tables[users]";

Works but..

"CREATE TABLE IF NOT EXISTS $tables['users']";

Does not.

I do not want to do this

$usersTable = $tables['users'];
"CREATE TABLE IF NOT EXISTS $usersTable";

I heard that it was considered bad practice to reference a key from an associative array without some sort of quotes around it. Is this true or is my first way of doing it preferred?

like image 922
John Smith Avatar asked Jul 29 '26 22:07

John Smith


1 Answers

You can do this with braces:

"CREATE TABLE IF NOT EXISTS {$tables['users']}";

Or through concatenation:

'CREATE TABLE IF NOT EXISTS ' . $tables['users'];
like image 117
Mark Elliot Avatar answered Aug 01 '26 12:08

Mark Elliot



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!