Jump to content

Math In Php Doesn't Do Math


Silvar

Recommended Posts

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?

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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. ;)

Link to comment
Share on other sites

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:

OQIN6.jpg

 

How do I get the 4 digit numbers to be at their right position?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.