从函数中获取值并在for循环外添加

时间:2016-09-07 13:36:02

标签: php for-loop return return-value

我有以下内容:

public function go(){
            for($x=0;$x<=31;$x++) {
                $this->get_answerrules($x);
                $DoneTime+=$DoneTime;
            }
            echo $gmdate("i:s", $DoneTime);;
        }

public function get_answerrules($x){
 ...
 ...
 ... 
        if($response = $this->request($data)){
            $obj = json_decode($response,true); 
                foreach($obj as $file) {
                        $Time += $file['batch_dura'];
                }
                $DoneTime = $Time;
                return $DoneTime;
       }else{}  
}

如何从31 for循环周期中获取值并将它们一起添加?

现在我的结果空白了。

1 个答案:

答案 0 :(得分:0)

您没有使用方法调用的结果:

public function go(){
    for($x=0;$x<=31;$x++) {
        $this->get_answerrules($x);
        $DoneTime+=$DoneTime;
    }
    // The below won't work as `$gmdate` is not in the scope of the method.
    echo $gmdate("i:s", $DoneTime);;
}

应该是这样的:

public function go() {
    // Initialize the variable
    $DoneTime = 0;
    for($x = 0; $x <= 31; $x++) {
        $DoneTime += $this->get_answerrules($x);
    }
    echo $gmdate("i:s", $DoneTime);
}