首页 > 解决方案 > 使用变量变量获取部分选项卡,并使用 php 将其设置为新变量

问题描述

我想问一下这段代码是否可行。

$allowed_section = ['what', 'how', 'rules'];

if (isset($_GET['section']) && in_array($_GET['section']), $allowed_section) {
   $section = $_GET['section'];

} else {
   $$_GET['section'] = "what";
}

如何在 HTML 中将 $$_GET['section'] 值声明为 $what 或 $how 或 $rules?有可能吗?

示例 HTML:

<div class = "tab-content">
    <div class="tab-pane fade show  <?=($what)? "active" : ""; ?>" 
         id="what" role="tabpanel" aria-labelledby="what-tab"> ...  </div>
    <div class="tab-pane fade show  <?=($how)? "active" : ""; ?>" 
         id="how" role="tabpanel" aria-labelledby="how-tab"> ...  </div>
    <div class="tab-pane fade show  <?=($rules)? "active" : ""; ?>" 
         id="rules" role="tabpanel" aria-labelledby="rules-tab"> ...  </div>
</div>

谢谢!

标签: phphtmlvariablestabsdynamic-variables

解决方案


这是可能的,但你必须扭转你的分配。

$allowed_section = ['what', 'how', 'rules'];

if (isset($_GET['section']) && in_array($_GET['section'], $allowed_section)) {
    $$_GET['section'] = true;

} else {
    $section = $_GET['section'];
}

推荐阅读