使用不同方法将codeigniter变量作为变量

时间:2020-08-31 08:33:47

标签: php codeigniter codeigniter-3

$i = 2;

$this->variable2 = "test"; // $this->variable2 is defined

$this->variable= '$this->variable'.$i;

echo ${$this->variable};

错误

遇到PHP错误

严重性:通知

消息:未定义的变量:$ this-> variable2

1 个答案:

答案 0 :(得分:-1)

这种使用不同类型用途的方法。

最后我找到了解决方法

在一种函数方法中创建变量

使用其他函数方法更新变量

使用不同函数方法调用变量

但是绝对不要直接调用变量名!!

public function BWT_start($i){
    global $varx;
    
    $varx = 'var'.$i;
    
    return $varx;
}

// Call Global Variable With Different Method with Never call Variable name Directly!!!.
public function BWT_run(){
    
    global $varx;
    
    for($i=1;$i<=7;$i++){
        $this->BWT_start($i); //echo Show created Variable Name

        $this->BWT_update($i); //echo Show updated Variable value
        
        $this->BWT_process($i); //echo Show Again Changed Variable value
    }
}

public function BWT_update($i){
    global $varx; global $$varx;
    
    $$varx = "DB_ROW_$i<br>";

    return $$varx;
    // 
}

public function BWT_process($i){
    $vary = 'var'.$i;
    global $$vary;
    $$vary = "DB_UPDATE_$i<br>";
    return $$vary;
}
相关问题