bukwus Posted March 5, 2012 Share Posted March 5, 2012 Hi I have built a series of forms with PHP and have been using SESSIONS to carry values from one to another. In one, the value of a variable is determined by adding the values of several other variables together: $totalFixed = $fixedCost1 + $fixedCost2 + $fixedCost3 + $fixedCost4 + $fixedCost5; This equation is working fine on that page. I then place the value of $totalFixed in a SESSION variable: $_SESSION['totalFix']=$totalFixed; I then assign this SESSION variable's value to a new variable on another page in the series: $dataValue[4] = $_SESSION['totalFix']; The number displays fine on this page as well. However, when the value is above 1,000 and I try to subtract another number from it, it ignores everything to the right of the comma. For example, if $dataValue[4] is 10,132 and $dataValue[5] is 25 and I subtract $dataValue[4] from $dataValue[5], the result is -15. Does anyone know the reason for this? Many thanks, Andy Quote Link to comment https://forums.phpfreaks.com/topic/258335-equation-breakdown/ Share on other sites More sharing options...
xyph Posted March 5, 2012 Share Posted March 5, 2012 Commas are added for human readability. They do not exist in computing. You need to remove these commas before attempting to perform arithmetic operations on the values. str_replace might help with this. Also, you need to be careful passing form data through sessions. What happens when you get half way through the form, open a new tab, and access the same form, or perhaps use a multi-page form with similar-named values? In order to keep the value unique to the request, you need to create a unique token that gets passed through a hidden field in the form. You then use the token to store the data in a unique location within your session. $_SESSION['formData'][$token]... Quote Link to comment https://forums.phpfreaks.com/topic/258335-equation-breakdown/#findComment-1324245 Share on other sites More sharing options...
smerny Posted March 5, 2012 Share Posted March 5, 2012 where is it getting the value with the commas in the first place? if you are storing numeric values you might as well store them as a number (without commas). if you want to have commas in output, i'd still store without and just use number_format() when you are going to echo something Quote Link to comment https://forums.phpfreaks.com/topic/258335-equation-breakdown/#findComment-1324248 Share on other sites More sharing options...
bukwus Posted March 8, 2012 Author Share Posted March 8, 2012 Thank you everyone. This was very helpful. Quote Link to comment https://forums.phpfreaks.com/topic/258335-equation-breakdown/#findComment-1325207 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.