# *solved* variable comparison/logical operator problem

12 replies to this topic

### #1 Switch0r

Switch0r
• Members
• 33 posts
• LocationManchester, UK

Posted 12 June 2006 - 10:59 PM

hey peeps, i wonder if anyone can help me with my predicament...im sure someone can, cos it should be fairly simple but the answer keeps evading me...

here goes.

i have some variables sent from a form, 10 in total, and i want to make sure that they meet a required level. i have var 1, and var a > i. I want either var 1 to equal 1 & a > i to be 0, or var 1 to be 0 and at least one of a > i to be 1, but not both at the same time...if that makes any sense at all

any thoughts? this is driving me crazy
The Human Torch was denied a bank loan...

### #2 poirot

poirot
• Members
• 646 posts
• LocationAustin, TX

Posted 13 June 2006 - 12:50 AM

That sounded a bit confusing, but I guess you may be looking for the XOR operator.

[a href=\"http://www.php.net/manual/en/language.operators.logical.php\" target=\"_blank\"]http://www.php.net/manual/en/language.operators.logical.php[/a]

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Xor - TRUE if either \$a or \$b is TRUE, but not both [/quote]
~ D Kuang

### #3 Switch0r

Switch0r
• Members
• 33 posts
• LocationManchester, UK

Posted 13 June 2006 - 06:33 AM

well thats what i thought, and i'd been trying various methods of doing that

what i had so far is this: id make a check if var a > i is set to 1, and if so, update a check variable, to make sure at least one of them is set, then compare this check var with the value of var 1

`if(\$var 1 < 1 xor \$check_var < 1){update a second check variable}`

but either way it comes up with the same answer, with no vars selected, 1 or the other, or all selected...
The Human Torch was denied a bank loan...

### #4 redarrow

redarrow
• Members
• 7,308 posts
• Locationlondon

Posted 13 June 2006 - 06:46 AM

[!--quoteo(post=383120:date=Jun 13 2006, 06:33 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 06:33 AM) [/div][div class=\'quotemain\'][!--quotec--]
well thats what i thought, and i'd been trying various methods of doing that

what i had so far is this: id make a check if var a > i is set to 1, and if so, update a check variable, to make sure at least one of them is set, then compare this check var with the value of var 1

`if(\$var 1 < 1 xor \$check_var < 1){update a second check variable}`

but either way it comes up with the same answer, with no vars selected, 1 or the other, or all selected...
[/quote]

```if(\$var 1 =< 1 xor \$check_var =< 1){update a second check variable}
```

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

### #5 Switch0r

Switch0r
• Members
• 33 posts
• LocationManchester, UK

Posted 13 June 2006 - 07:19 AM

well ive changed the plan slightly...hopefully less complicated now

i now have 9 checkboxes on the form, and at least one of them needs to be checked to proceed.

this is what ive got so far:
```// setup
\$count = 0;
if(\$var1 = 1){\$count++;}
if(\$var1 = 2){\$count++;}
if(\$var1 = 3){\$count++;}
if(\$var1 = 4){\$count++;}
if(\$var1 = 5){\$count++;}
if(\$var1 = 6){\$count++;}
if(\$var1 = 7){\$count++;}
if(\$var1 = 8){\$count++;}
if(\$var1 = 9){\$count++;}

// check
if(\$count = 0){Give error}
```

only this still doesnt work
The Human Torch was denied a bank loan...

### #6 d_barszczak

d_barszczak
• Members
• 188 posts

Posted 13 June 2006 - 07:25 AM

[!--quoteo(post=383132:date=Jun 13 2006, 08:19 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 08:19 AM) [/div][div class=\'quotemain\'][!--quotec--]
well ive changed the plan slightly...hopefully less complicated now

i now have 9 checkboxes on the form, and at least one of them needs to be checked to proceed.

this is what ive got so far:
```// setup
\$count = 0;
if(\$var1 = 1){\$count++;}
if(\$var1 = 2){\$count++;}
if(\$var1 = 3){\$count++;}
if(\$var1 = 4){\$count++;}
if(\$var1 = 5){\$count++;}
if(\$var1 = 6){\$count++;}
if(\$var1 = 7){\$count++;}
if(\$var1 = 8){\$count++;}
if(\$var1 = 9){\$count++;}

// check
if(\$count = 0){Give error}
```

only this still doesnt work
[/quote]

Your if statement is wrong here you need if(\$var1 == 9){\$count++;} note the 2 == otherwise your applying a new value to \$var1.

When all else fails - Try reading the manual.

Integrated IT Systems Ltd

### #7 redarrow

redarrow
• Members
• 7,308 posts
• Locationlondon

Posted 13 June 2006 - 07:26 AM

You have to use == as equal or you are setting the vars as new varable names.

was betten dam lol

```// setup
\$count = 0;
if(\$var1 == 1){\$count++;}
if(\$var1 == 2){\$count++;}
if(\$var1 == 3){\$count++;}
if(\$var1 == 4){\$count++;}
if(\$var1 == 5){\$count++;}
if(\$var1 == 6){\$count++;}
if(\$var1 == 7){\$count++;}
if(\$var1 ==8){\$count++;}
if(\$var1 ==9){\$count++;}

// check
if(\$count == 0){Give error}

```

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

### #8 Switch0r

Switch0r
• Members
• 33 posts
• LocationManchester, UK

Posted 13 June 2006 - 07:28 AM

well ive got them all with == instead, and no matter how many boxes i choose (other than 0), the \$count remains at 0, muh?
The Human Torch was denied a bank loan...

### #9 d_barszczak

d_barszczak
• Members
• 188 posts

Posted 13 June 2006 - 07:31 AM

[!--quoteo(post=383135:date=Jun 13 2006, 08:28 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 08:28 AM) [/div][div class=\'quotemain\'][!--quotec--]
well ive got them all with == instead, and no matter how many boxes i choose (other than 0), the \$count remains at 0, muh?
[/quote]

ok,

as a test you could put a echo var1 to make sure that the variable is being passed.

think you might need something like.

```\$var1 = \$_POST['var1'];
```

When all else fails - Try reading the manual.

Integrated IT Systems Ltd

### #10 redarrow

redarrow
• Members
• 7,308 posts
• Locationlondon

Posted 13 June 2006 - 07:33 AM

[!--quoteo(post=383135:date=Jun 13 2006, 07:28 AM:name=Switch0r)--][div class=\'quotetop\']QUOTE(Switch0r @ Jun 13 2006, 07:28 AM) [/div][div class=\'quotemain\'][!--quotec--]
well ive got them all with == instead, and no matter how many boxes i choose (other than 0), the \$count remains at 0, muh?
[/quote]

Try like this for a moment what happens.

```<?

echo \$var1;

if(\$_POST('\$submit') {

\$count = 0;
if(\$var1 == 1){\$count++;}
if(\$var1 == 2){\$count++;}
if(\$var1 == 3){\$count++;}
if(\$var1 == 4){\$count++;}
if(\$var1 == 5){\$count++;}
if(\$var1 == 6){\$count++;}
if(\$var1 == 7){\$count++;}
if(\$var1 ==8){\$count++;}
if(\$var1 ==9){\$count++;}

if(\$var1 ==0){echo " Sorry i am not working!";}
}
?>
```

Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

### #11 d_barszczak

d_barszczak
• Members
• 188 posts

Posted 13 June 2006 - 07:47 AM

This should grab the posted variable and place it in a local variable.

to make sure the information has been passed you can delete the comment tags and the variable will be printed to the screen.

```// setup

\$var1 = \$_POST['var1'];

// echo "\$var1";

\$count = 0;

if(\$var1 == 1){\$count++;}
if(\$var1 == 2){\$count++;}
if(\$var1 == 3){\$count++;}
if(\$var1 == 4){\$count++;}
if(\$var1 == 5){\$count++;}
if(\$var1 == 6){\$count++;}
if(\$var1 == 7){\$count++;}
if(\$var1 == 8){\$count++;}
if(\$var1 == 9){\$count++;}

// check
if(\$count == 0){Give error}
```

When all else fails - Try reading the manual.

Integrated IT Systems Ltd

### #12 poirot

poirot
• Members
• 646 posts
• LocationAustin, TX

Posted 13 June 2006 - 01:26 PM

Wouldn't this block of code do the same as:

```if ((int) \$_POST['var1'] <= 0) {
// give error
} else {
\$count = 1;
}```

~ D Kuang

### #13 Switch0r

Switch0r
• Members
• 33 posts
• LocationManchester, UK

Posted 13 June 2006 - 07:15 PM

right, well...ive sorted it

added the \$_POST array vars for the comparison, but changed the check so it checks if its "on" or not, as i had previously thought that checkboxes gave either 1 (checked) or 0 (blank), which apparently is untrue as it comes up as: on (checked) or empty var (blank)

thanks for the help tho peeps [img src=\"style_emoticons/[#EMO_DIR#]/wink.gif\" style=\"vertical-align:middle\" emoid=\":wink:\" border=\"0\" alt=\"wink.gif\" /]
The Human Torch was denied a bank loan...

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users