PHP Sum Variable in While Loop

php sum variable in while loop

Simply initialize a variable outside your loop for example:

$total_price = 0;

and increment this number inside your loop:

$total_price += $row['price'] * $row['order_q'];

SUM values inside while loop

Declare $total outside while loop as $total=0, write $total=$total+ ($order*$duration) inside loop

PHP - How to use the sum variable inside a loop

if you want the total sum of your $value inside the loop, you must first calculate it, before entering the loop:

$sum = array_sum(array_column($group, 'column_key'));

where 'column_key' is the index of the array $group containing the values you want to sum.

Example:

Let's say that we have this array containing values to sum:

$data = [
[
'id' => 2,
'name' => 'Jhon',
'salary' => 1500
],
[
'id' => 5,
'name' => 'Jane',
'salary' => 2000
]
];

Now we want the sum of all salaries:

$totSalary = array_sum(array_column($data, 'salary'));
// echo $totSalary will print 3500

SUM in a while loop - php

$total=0;
while($row = $result->fetch_assoc()) {

$sum= $row["num1"] + $row["num2"];

$total = $total + $sum;

}

Get the sum of the value of a variable in a PHP while loop

To get the global total price you can:

set a variable $sub_total before the while as follow:

$sub_total = 0;

In the while loop you add to your variable the $total_price calculated for each iteration as follow:

$sub_total = $sub_total + $total_price

or in compact way:

$sub_total += $total_price

An advice:

change this:

$total_price = sprintf("%.02f", $item_price * $item_qty);

in:

$total_price = $item_price * $item_qty;

and format your value when you want to display.

Other code parts shoud be changed, but I focalized my attention on your main request.

Unable to do sum and getting same value by using while loop php

You're feeding the same hard-coded values to the summe function each time, so it's unsurprising that you get the same results each time. (I assume that function just adds the two numbers together, although you didn't show the code for it.)

If you want it to increment each time, then you need to keep a record of the value returned on the previous loop, and use that instead. Something like this:

$i = 1;
$startVal = 35501;
$endVal = 36000;
$amount = 3575;

while( $i <= 65 ) {
$startVal = $this->summe($startVal, 500);
$endVal = $this->summe($endVal, 500);
$amount = $this->summe($amount, 50);
echo "array(<br>";
echo "'low' => '".$startVal."',<br>";
echo "'high' => '".$endVal."',<br>";
echo "'amount' => '".$amount."',<br>";
echo '\'city\' => $city<br>';
echo "),<br>";
$i++;
}

Demo: http://sandbox.onlinephpfunctions.com/code/e8bcca803d8cac56b85d5ac7841d18250aca11e3

Also the summe function is redundant, you don't need to have a wrapper just for doing a trivial addition. Without the unncessary overhead of a function call, you could write the same 3 lines as

$startVal += 500;
$endVal += 500;
$amount += 50;

instead.


P.S. As an aside, you appear to be trying to use PHP to generate string output similar to that produced by print_r() - or perhaps more like var_export(). It's not clear why you're doing that rather than just using one of those debugging functions directly, or (if debugging isn't the objective) outputting to a recognised, standard, parseable format such as JSON or XML.

How to sum numbers with php using for loop?

Since you specifically said for loop:

<?php

$start = 1;
$end = 10;

$sum = 0;
for ($i = $start; $i <= $end; $i++) {
$sum += $i;
}

echo "Sum from " . $start . " to " . $end . " = " . $sum;


Related Topics



Leave a reply



Submit