Bricktop Posted August 5, 2009 Share Posted August 5, 2009 Hi all, I have a foreach loop which extracts a price from an array and outputs it every time a match is found. The price is stored as $price. What I would like to do is add the $price variable together on each loop and store the number in $total. How would I achieve this please? My code is as follows: foreach($_POST['order_price'] as $value) { $prices = ""; foreach($domainprices as $tld=>$price) { if(substr($value, -strlen($tld)) == $tld) { $prices = $price; break; } } $content .='<div>'; $content .='<p><span>'.$value.'</span>'; $content .='<span>'.$price.'</span></p>'; $content .='</div>'; } The prices are all stored as; 12.99, 9.99, 14.99, 15.99 etc etc Many thanks! Quote Link to comment Share on other sites More sharing options...
bundyxc Posted August 5, 2009 Share Posted August 5, 2009 $total += $price; This will add the current value for $total to the current value for $price In essence, it's shorthand for: $total = $total + $price; Quote Link to comment Share on other sites More sharing options...
Bricktop Posted August 5, 2009 Author Share Posted August 5, 2009 Perfect! Thanks bundyxc. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.