devendra devendra - 2 months ago 6
PHP Question

Addition of Value Stored in Variable in a Loop

I have a foreach loop. which has a variable. how to Add variable values in a single variable.

$sal = "";
foreach($variable as $key => $value){
$sal= $value->Salary;
}
echo $sal;

Answer

try this

$sal = 0;
foreach($variable as $key => $value){
   $sal += $value->Salary;
}
echo $sal;
Comments