从字符串内部引用关联数组? [英] Reference an associative array from inside a string?

查看:59
本文介绍了从字符串内部引用关联数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

可以,但是..

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

不是.

我不想这样做

$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']}";

或通过串联:

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

这篇关于从字符串内部引用关联数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆