Silvar Posted January 1, 2013 Share Posted January 1, 2013 I've got this code in my script: $mathShit = $listAddupAmount.$gWithdrawDeposit.$gAmount; The $gWithdrawDeposit variable is either + or -. As an example if $listAddupAmount was 5500 and $gAmount was 1000, $mathShit would output 5500-1000 and not 4500. How do I do this? Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/ Share on other sites More sharing options...
kicken Posted January 1, 2013 Share Posted January 1, 2013 You have to use conditionals to determine what to do, you can't just concatenate things together and expect PHP to figure it out. if ($gWithdrawDeposit == '+') $mathShit = $listAddupAmount + $gAmount else if ($gWithdrawDeposit == '-') $mathShit = $listAddupAmount - $gAmount Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402495 Share on other sites More sharing options...
Christian F. Posted January 1, 2013 Share Posted January 1, 2013 This doesn't to what you think it does, as you've noticed already. By using the concatenation operator (a period) between the variables you're treating them as strings, or said in other words: You're gluing text literals together. PHP has no concept of what these strings are, nor does it care. It just does what you tell it to. Same as writing "5500-1000" on a piece of paper, really. As Kicken just posted, while I was writing this, you need to tell PHP what the content of the variables means. If you want something to happen on the basis of the input. I would recommend that you read more up on strings in PHP, and basic PHP in general. Once you've wrapped your head around the basic syntax, operator types and value types, things should be a lot more logical for you. Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402497 Share on other sites More sharing options...
Silvar Posted January 1, 2013 Author Share Posted January 1, 2013 I fixed the problem by making this: $theArray = array($listAddupAmount, $gWithdrawDeposit.$gAmount); $doTheMath = array_sum($theArray); echo $doTheMath; Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402502 Share on other sites More sharing options...
Silvar Posted January 1, 2013 Author Share Posted January 1, 2013 You have to use conditionals to determine what to do, you can't just concatenate things together and expect PHP to figure it out. if ($gWithdrawDeposit == '+') $mathShit = $listAddupAmount + $gAmount else if ($gWithdrawDeposit == '-') $mathShit = $listAddupAmount - $gAmount I got another problem now. The ordering is wrong when i do "ORDER BY amount DESC". It's like the biggest number get moved a row down, because it's 1 digit longer than the others. Illustration here: How do I get the 4 digit numbers to be at their right position? Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402505 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You make them actual numbers instead of varchar. Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402508 Share on other sites More sharing options...
Silvar Posted January 1, 2013 Author Share Posted January 1, 2013 You make them actual numbers instead of varchar. Thanks! Simple and easy. Wasn't even my intention to use VARCHAR. I was gonna use INT (11). Quote Link to comment https://forums.phpfreaks.com/topic/272565-math-in-php-doesnt-do-math/#findComment-1402509 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.