简体   繁体   English

如何计算总数?

[英]How do I count the total of numbers?

I am looking for a way to get the sum of a values. 我正在寻找一种获取值总和的方法。 I have this <nbsection>2</nbsection><nbsection>4</nbsection> 我有这个<nbsection>2</nbsection><nbsection>4</nbsection>

So I do this loop to get the values of the nbsection but I don't have the sum: 因此,我执行此循环以获取nbsection的值,但是我没有总和:

$countvaluesection=0;

foreach($getneb as $clé=>$value){

    $countvaluesection+=$value->nbsection)
    var_dump($countvaluesection)

}

I have on var_dump 2 and 4, not the total. 我有var_dump 2和4,而不是总数。 How can I get the total? 我如何获得总数?

Thanks in advance! 提前致谢!

That can't be your code because you have multiple syntax errors. 那不是您的代码,因为您有多个语法错误。 This has an end paren without a beginning paren and is missing a semicolon: 它的结尾括号没有开头的括号,并且缺少分号:

$countvaluesection+=$value->nbsection)

This is missing a semicolon: 这缺少分号:

var_dump($countvaluesection)

This works just fine: 这很好用:

$countvaluesection = 0;

foreach($getneb as $clé => $value){
    $countvaluesection += $value->nbsection;
}

Given that $value has a property nbsection that is a numerical value. 假定$value具有nbsection属性,该属性是一个数值。 Under no circumstances would the value of nbsection be assigned to $countvaluesection when you're using the += operator. 使用+=运算符时,在任何情况下nbsection的值nbsection不会分配给$countvaluesection

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM