How to get the sum value of a variable?

I have a variable inside a foreach loop and i want to add or sum the numbers,

echo $variable

outputs 19.9614.95

the numbers need to be added together like 19.96 + 14.95 so the output would be:

How do you do this?

Post your code.



<?php $order = Mage::getSingleton('sales/order'); $order->loadByIncrementId( Mage::getSingleton('checkout/session')->getLastRealOrderId() ); $totalData = $order->getData(); $allitems = $order->getAllVisibleItems(); $ShippingAmount = $order->getShippingAmount(); $itemsCollection = $order->getItemsCollection(); $total_ordered_items = $order->getData('total_qty_ordered'); $num_of_ordered_items = 0; $supplier_shipping_cost = 0; foreach ($itemsCollection as $item){ //$num_of_ordered_all_items += $item->getQtyOrdered(); // Number of all item ordered //$num_of_shipped_items += $item->getQtyShipped(); //$item->getQtyInvoiced() $product = Mage::getModel('catalog/product')->load($item->getProductId()); $supplier_shipping_cost = $product->getData('supplier_shipping_cost'); $cost = $product->getData('cost'); $price = $product->getData('price'); $num_of_ordered_items = number_format((float)$item->getQtyOrdered()); $total_cost = $cost * $num_of_ordered_items; $total_price = $price * $num_of_ordered_items; $subtotal_profit = $total_price - $total_cost; $profit_foreach_group = $subtotal_profit - $supplier_shipping_cost; echo $profit_foreach_group; //$a = array_sum($profit_foreach_group) //echo array_sum($profit_foreach_group) } ?>[/code]

There is no variable named $variable in your code so how are you getting an output when you echo it?

the numbers need to be added together

What numbers?

If you want help you need to post a proper question.

I was just using $variable as an example.

What I’m trying to add in this code is $profit_foreach_group;

I can’t see how because there is no comma between the arrays.

I’m trying to add all the profit I can! :slight_smile:

Before the loop, add a line that says

$totalprofit = 0;

Inside the loop, after you’ve assigned a value to $profit_foreach_group, add a line

$totalprofit += $profit_foreach_group;

Echo that variable at the end of the loop.

Or, if you want to create an array for some reason, change your line to

$profit_foreach_group[] = $subtotal_profit - $supplier_shipping_cost;

which will add a new element to the array each time the loop iterates. You can then sum the array at the end of the loop. Not sure it makes it any easier, and you can’t echo $profit_foreach_group if you make it into an array.

Seeing error message(s) could be a help.

But going by what you’re getting it looks like values are getting contenated as strings rather than added as numbers. PHP does some type juggling so there might be some type casting needed.

I’m not seeing how an array function would work with either strings or numerics. Wouldn’t simple addition work OK?

Thanks droopsnoot that got it.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.