本文介紹了while循環(huán)中的php sum變量的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我必須在 while 中求和"變量的值,這里是我的示例:
I have to "sum" variable's values in while, here us my example :
while($row = mysql_fetch_array($result)){
$price= $row['price'] * $row['order_q'];
}
如果我輸入echo $price;
,上面的代碼會輸出例如:
The code above will output if I put echo $price;
for example:
1915201310
我想要類似:sum($price)
或 array_sum($price)
來計算 while 循環(huán)的所有結(jié)果.所以,我想數(shù):19+15+20+13+10 = 77
I want something like : sum($price)
or array_sum($price)
to count all the results of while loop. So, that i want to count: 19+15+20+13+10 = 77
我如何用 php 做到這一點?
How can I do it with php?
謝謝
推薦答案
只需在循環(huán)外初始化一個變量,例如:
Simply initialize a variable outside your loop for example:
$total_price = 0;
并在循環(huán)中增加這個數(shù)字:
and increment this number inside your loop:
$total_price += $row['price'] * $row['order_q'];
這篇關(guān)于while循環(huán)中的php sum變量的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!