tags:

views:

323

answers:

5

I have two variables:

$qty = 7;
$_POST['qty'] = 6;

var_dump($qty, $_POST['qty']); // both vars are integers
$_SESSION['qty'] = $qty + $_POST['qty'];
echo '='.$_SESSION['qty'];

This returns:

int(7) int(6) =1

(int)$qty, (int)$_POST['qty'] doesn't solve the problem.

What am I doing wrong?

Update:

... intval($qty) + intval($_POST['qty']);

not help.

And I notice one more detail. Problem apeear only when $_SESSION['qty'] >= 10:

$_SESSION['qty'] = $qty + $_POST['qty']; // $qty = 3, $_POST['qty'] = 6

Return good result ($_SESSION['qty'] = 9).

SOLVED

Thanks all for your unswers. But problem more not actual (it was a server problem). Anyway +1 to all.

+1  A: 

Very odd indeed, I've never had this sort of problem. If you explicitly use the integer values in the arithmetic operation? (Not the same as casting to integer)

$_SESSION['qty'] = (intval($qty) + intval($_POST['qty']));

Have you tried using only your own variables, and leaving the POST-value out of the operation?

Björn
intval() not help... And, problem not in $_POST, tried to do it with test constants without resul.
ARTstudio
+1  A: 

What you're doing with (int) is casting the variable, not converting it. You should use intval($var) instead.

See PHP: Integers.

Lachlan McDonald
Anyway this give me no results.
ARTstudio
there's no diff between int and intval actually
stereofrog
`(int) $x` doesn't change the value of x.
nickf
+1  A: 

What does this give you?

$localQty = 7;
$_POST['qty'] = 6;

$_SESSION['qty'] = ($localQty + $_POST['qty']);
var_dump($_SESSION['qty']);

Do you initialize your Session properly with session_start() at the beginning of your script?

Alex
+9  A: 

Your $_SESSION is implicitly initialized as string somewhere

$_SESSION = 'blah';

$_SESSION['qty'] = 13;
var_dump($_SESSION['qty']); // gives "1"
stereofrog
This seems to be the case. Make sure you have session_start() at the beginning of your script, and that you aren't assigning the $_SESSION variable (rather than one of its index values) to a string somewhere.
Dustin Fineout
Good catch. Sometimes answers need a lot of imagination.
ntd
+1  A: 

I would print_r($_SESSION) to see what it is.

MathGladiator