Page 1 of 1

How can i add numerics in conditional if statement?

Posted: Wed Jun 27, 2007 8:40 am
by jramaro
I made a drop down menu and i need script to check the value and add +35 under certain condition

in one of drop down options the value = "22". if that one isnt selected it should add 35 (numeric) to all the others .

Code: Select all

if($_POST['menu1'] != '22') {  
$total + 35 ; 
}
when i use echo it shows that it will only add 35 when value is not '22' so it seemed to be working as for the logical side
but when i do it like the above it wont show up in my echo $total.
Can i not add numeric values to variables that way? It's giving really high and crazy numbers instead of nothing .

Re: How can i add numerics in conditional if statement?

Posted: Wed Jun 27, 2007 9:06 am
by volka
You have to re-assign the newly computed value to $total

Code: Select all

$total = $total + 35; 
// or shorter
$total += 35;

Posted: Wed Jun 27, 2007 9:15 am
by mikeeeeeeey
listen to volka, he has the right idea.

also, just thought Id mention to avoid catastrophe...

Code: Select all

if($_POST['menu1'] != '22') {}
you do know that you're matching the form variable to a string of 22, rather than the integer 22?

and also, if you want to make a direct comparison use either...

Code: Select all

if($_POST['menu1'] !== 22) {}
// or
if(!$_POST['menu1'] == 22) {}
hope this helps!

Posted: Wed Jun 27, 2007 9:18 am
by volka
echo gettype($_POST['menu1']); will print string.
The POST parameters always ship as strings.
Therefore even

Code: Select all

if($_POST['menu1'] !== '22') {
would work ;)

Posted: Wed Jun 27, 2007 9:28 am
by jramaro
thank you I will try that