-2

i make 2 arrays in PHP and i added value in array here

$num_a = array(2,4);
$num_b = array(8,12);

i make a conditional for calculate total value in array like this using foreach loop like this, i declared the total = 0 so that there are no random numbers in it

    $total_a = 0;
    $total_b = 0;
    foreach($num_a as $key => $a){
        $total_a += $a;
        $total_b += $num_b;
    }

so i check the result but it's have an error

$result = $total_a/$total_b;

the error is : Fatal error: Uncaught DivisionByZeroError: Division by zero in line 52
i know the error because it's cannot doing operation zero in value variable, but here i have fill the value using foreach loop, so how it can be? and how to solve this, thank you...

1 Answers1

0

You should replace

$total_b += $num_b;

by

$total_b += $num_b[$key];
Fravel
  • 70
  • 2
  • 7
  • 1
    this is more of a comment than and answer, leaving a pointer to a potential typo by the original author (the answer is to not make typos which is too broad for this sites scope). – hakre Jun 30 '22 at 08:47