2

"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?

2
  • If it was that bad it wouldn't work. Commented Apr 6, 2011 at 2:08
  • Then is it not considered bad practice? I'd rather use that method as it requires the least markup but I thought there was a reason for not using it. Commented Apr 6, 2011 at 2:58

3 Answers 3

8

You can do this with braces:

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

Or through concatenation:

'CREATE TABLE IF NOT EXISTS ' . $tables['users'];
Sign up to request clarification or add additional context in comments.

Comments

1

You can use curly brackets.

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

Comments

1

You could do the folling:

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

// do some other stuff

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.