“致命错误:未划分的划分划分:零”。但是变量已添加了值

发布于 2025-02-11 18:16:42 字数 507 浏览 1 评论 0原文

我在php中制作了2个数组,在此处添加了数组的值

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

,我在这样的段落中使用foreach循环来计算阵列中的总值其中的数字,

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

所以我检查结果,但是有错误的

$result = $total_a/$total_b;

错误是: 致命错误:未划分的划分ByzeroError:第52行中的零分割
我知道错误是因为它不能在值变量中进行零操作,但是在这里我使用foreach循环填充了值,那么它可以如何?以及如何解决这个问题,谢谢...

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

迷途知返 2025-02-18 18:16:42

你应该替换

$total_b += $num_b;

$total_b += $num_b[$key];

You should replace

$total_b += $num_b;

by

$total_b += $num_b[$key];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文