I want to show Total Salary, which is the sum of all employee's net salary. How can I do that? I have tried some code but its not working

<tbody>
<?php
$salary = 0;
if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):
$salary = $salary + $v_emp_salary->basic_salary;
?>
<tr>
<td><?php echo $v_emp_salary->employment_id; ?></td>
<td><?php echo $v_emp_salary->first_name . ' ' . $v_emp_salary->last_name ?></td>
<td><?php echo $gross = $v_emp_salary->basic_salary + $v_emp_salary->house_rent_allowance + $v_emp_salary->medical_allowance + $v_emp_salary->special_allowance + $v_emp_salary->fuel_allowance + $v_emp_salary->phone_bill_allowance + $v_emp_salary->other_allowance ?></td>
<td><?php echo $deduction = $v_emp_salary->tax_deduction + $v_emp_salary->provident_fund + $v_emp_salary->other_deduction ?></td>
<?php $net_salary = $gross - $deduction ?>
<td><?php echo $net_salary ?></td>
<td><?php
if ($v_emp_salary->employment_type == 1) {
echo 'Provision';
} else {
echo 'Permanent';
}
?></td>
<td><?php echo btn_view('admin/payroll/view_salary_details/' . $v_emp_salary->employee_id); ?></td>
<td>
<?php echo btn_edit('admin/payroll/manage_salary_details/' . $v_emp_salary->employee_id . '/' . $v_emp_salary->designations_id); ?>
</td>
</tr>
<?php endforeach; ?>
<?php endif; ?>
</tbody>
this is code I have tried for sum the net salary
<?php
$salary = 0;
if (!empty($emp_salary_info)):foreach ($emp_salary_info as $v_emp_salary):
$salary = $salary + $v_emp_salary->basic_salary;
echo 'Total Salary :'
?>
<?php echo $salary; ?>