foreach (range(1,$max_days, 1) as $days){ $total = 0 echo $total; foreach($model->fetchSomefruits($days) as $fruit){ if($fruit == 'fresh') $total+=2; elseif($fruit == 'rotten') $total-=1; else $total+=1; } echo $total; //How can I go back and have the same value as I have now? }