Frank Thornton Frank Thornton - 2 months ago 6
PHP Question

Getting value from function and adding outside of for loop

I have the following:

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


and

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{}
}


How do I get the value from the 31 for loop cycles and add them together?

Right now my results are coming up blank.

Answer

You are not using the results of your method call:

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);;
}

Should probably be something like:

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