How can i add numerics in conditional if statement?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

How can i add numerics in conditional if statement?

Post 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 .
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

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

Post by volka »

You have to re-assign the newly computed value to $total

Code: Select all

$total = $total + 35; 
// or shorter
$total += 35;
User avatar
mikeeeeeeey
Forum Contributor
Posts: 130
Joined: Mon Jul 03, 2006 4:17 am
Location: Huddersfield, UK

Post 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!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 ;)
jramaro
Forum Commoner
Posts: 58
Joined: Tue Jun 26, 2007 7:46 am

Post by jramaro »

thank you I will try that
Post Reply