开发者

Reference an associative array from inside a string?

开发者 https://www.devze.com 2023-02-22 03:07 出处:网络
\"C开发者_StackOverflowREATE TABLE IF NOT EXISTS $tables[users]\"; Works but.. \"CREATE TABLE IF NOT EXISTS $tables[\'users\']\";

"C开发者_StackOverflowREATE 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?


You can do this with braces:

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

Or through concatenation:

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


You could do the folling:

$query = sprintf("CREATE TABLE IF NOT EXISTS %s", $tables['users']);

// do some other stuff


You can use curly brackets.

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号