括号内的php变量[关闭]

php variable within bracket

What Im trying to do is pull page titles names from DB depending on City

example

$city = "Houston";
$title = $settings['PageTitle$city'];

I get no result even though $settings['PageTitleHouston'] exists in the database

using $settings['PageTitleHouston'] works but I Need $city in the variable as I am doing many cities.

Any suggestions?

( I did search about 30 minutes for answers)

You're very close:

$title = $settings['PageTitle'.$city];

you could also use double quotes:

$title = $settings["PageTitle$city"];

Take a look at the variable parsing page on the PHP manual for more information about PHP and double quotes.

Don't use single quotes. The php parser will not substitute variable values in single quoted strings. Your code ought to work by simply substituting double quotes for the single.

$title = $settings["PageTitle$city"];

or, if you want to be more explicit

$title = $settings["PageTitle{$city}"];

You can also concatenate the strings using the period operator.

Try this:

$city = "Houston; $title = $settings['PageTitle'.$city];