FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Found the Problem, this topic can be closed... thanks =)





Star Wars Fanatic
EDIT: Found the problem, thanks for the help, though...

EDIT: See the third post for the updated problem!

Like the Title says, in one of my scripts, PHP isn't subtracting correctly.

Here is the code that I use:

Code:
$curExp = (int)slayerExp($row[0]);
$startExp = (int)$row[1];
$expGain = $curExp - $startExp;


The first line gets a number that is a string, so I convert it to an int, the second line gets a number from a MySQL query, that is an int in the database, but for some reason is a string in the array.

$curExp contains the number 57430.
$startExp contains the number 57400.

When I try to subtract (third line in the code) it just gives me the value -57400. If I do something like this:

Code:
$value = 57430 - 57400;


Then it correctly gives me the value 30.

Anyone got any ideas of why it is doing that?
mathiaus
Have you tested the vars?
Code:
var_dump($curExp);
var_dump($startExp);


My guess is that $curExp is returning 0, either because of the slayerExp function or the $row[0] value. I could be wrong but its good to check anyway Smile
Star Wars Fanatic
Yea, I had checked before but the var_dump allowed me to see a little bit more, and it seems that the "(int)" in the first line

Code:
$curExp = (int)slayerExp($row[0]);


Changes the value of $curExp to 0 for some reason.

I have to have that there, as the slayerExp function passes it a String.

After trying out a few things, it seems no matter what I do, using intval, or (int), it changes the value of the string to 0, and the string is numbers... Any ideas?

And thanks for your help so far Smile

Alright, I played with it some more, trying out the settype() function, so it seems that before it is converted (the $curExp variable) it contains the value:

Code:
string(29) "243963"


And after the conversion (using either (int), intval(), or settyoe()) the value is:

Code:
int(0)
Star Wars Fanatic
I found the problem, this topic can be closed now, thanks for your help, Mathiaus. Smile
Related topics
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.