StefanRSA Posted April 29, 2009 Share Posted April 29, 2009 I am getting numbers from a string and my code look like: foreach ($gresult as $x ){ if(!empty($x)){ $unit_result= explode(",",$x); $price = $unit_result[6]; echo $price; } } $price can contain 3000 and 1500 but if I echo it gives me 30001500. How can I ad $price to give me the total? (Like 3000 + 1500 = 4500) In other words. I just want the sum of $price. Can anybody help me out here please? Link to comment https://forums.phpfreaks.com/topic/156070-explode-strings-to-get-numbers-and-do-calculation/ Share on other sites More sharing options...
Mchl Posted April 29, 2009 Share Posted April 29, 2009 $sum = 0; foreach ($gresult as $x ){ if(!empty($x)){ $unit_result= explode(",",$x); $price = $unit_result[6]; $sum += $price; } } echo $sum; Link to comment https://forums.phpfreaks.com/topic/156070-explode-strings-to-get-numbers-and-do-calculation/#findComment-821611 Share on other sites More sharing options...
StefanRSA Posted April 29, 2009 Author Share Posted April 29, 2009 TY Mchl! Link to comment https://forums.phpfreaks.com/topic/156070-explode-strings-to-get-numbers-and-do-calculation/#findComment-821633 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.