PHP函数中的变量内部变量?

时间:2012-03-21 14:51:56

标签: php function variables

我有以下功能。

问题是$ lang变量会因$this->setts['site_lang'];而异。实际问题如下:

$condition_details['description_$lang']

(这不起作用)。如何显示

$condition_details['description_us']

$condition_details['description_fr']

取决于$ lang设置?

这是完整的功能:

function shipped_drop_down($box_name = 'item_shipped_via', $selected = null, $form_refresh = null)
{
    (string) $display_output = null;
    $lang = $this->setts['site_lang'];
    $sql_select_conditions = $this->query("SELECT id, description_".$lang." FROM " . DB_PREFIX . "advanced_shipping_carriers ORDER BY theorder,description_".$lang." ASC");
    $display_output = '<select name="' . $box_name . '" ' . (($form_refresh) ? 'onChange = "submit_form(' . $form_refresh . ', \'\')"' : '') . ' style="font-size:10px; width: 120px;"> ';
    while ($condition_details = $this->fetch_array($sql_select_conditions))
    {
        $display_output .= '<option value="' . $condition_details['id'] . '" ' . (($condition_details['id'] == $selected) ? 'selected' : '') . '>' . $condition_details['description_$lang'] . '</option> ';
    }
    $display_output .= '</select> ';
    return $display_output;
}

4 个答案:

答案 0 :(得分:5)

你可以做到

$condition_details['description_'.$lang]

或使用"

$condition_details["description_$lang"]

答案 1 :(得分:2)

您可以将语言设置存储在以下数组中:

$lang['fr']['condition_details']

所以你可以使用$lang[$selected_lang]['condition_details']

答案 2 :(得分:1)

使用“双引号”代替“单引号”。这样,$lang将被解析并替换为相关值。

答案 3 :(得分:0)

使用双引号:

$condition_details["description_$lang"]