0

I want to check my variable $id is equal 497 or equal 200 , if $id not equal 497 or $id not equal 200 then mail, but when i run the example as below, I set $id =497 by hand , but run the code ,it's print "not equal\n";

my $id = 497; if($id != 497 || $id != 200) { print "not equal\n"; } else { print "equal , not to mail\n"; } 
1
  • 2
    If you're comfortable with mathematical logic notation, you might want to read about De Morgan's laws. They describe how a condition like “equal to 497 or equal to 200” must be negated. In Perl code: !($x || $y) == !$x && !$y, and !($x && $y) == !$x || !$y. Commented Oct 18, 2018 at 7:10

1 Answer 1

6

It says not equal because $id != 200 is true. || returns true if either operand is true.

You want to say:

if ($id != 497 && $id != 200) 

so not equal is printed only when $id is neither 497 or 200.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.