Page 1 of 1

IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Fri Oct 09, 2009 4:14 pm
by mattmill30
Hi

I'm using the following if statement:

Code: Select all

if (!(isset($_SESSION['login_register.POST']['email']) || isset($_SESSION['login_register.POST']['password'])) || $_SESSION['login_register.POST']['email']) == "" || $_SESSION['login_register.POST']['password'] == "")
Am i correct in thinking its a shortened version of:

Code: Select all

if (!(isset($_SESSION['login_register.POST']['email'])[b][u]) || !([/u][/b]isset($_SESSION['login_register.POST']['password'])) || $_SESSION['login_register.POST']['email']) == "" || $_SESSION['login_register.POST']['password'] == "")
Thanks,

Matthew Millar

Re: IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Fri Oct 09, 2009 4:27 pm
by Mark Baker
You don't need all those brackets when you're using the same operator (OR or AND) for all of your conditions, they only make a difference when you're mixing ORs and ANDs.
(A or B or C) is the same as ((A or B) or C) is the same as (A or (B or C))
(A and B and C) is the same as ((A and B) and C) is the same as (A and (B and C))

Simply add brackets as an aid to readbility

But, when you mix operators:
((A and B) or C) is NOT the same as (A and (B or C))


However, to answer your question, the two statements aren't synonymous
One of them has an additional !

Re: IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Fri Oct 09, 2009 5:46 pm
by mattmill30
Hi Mark,

Thanks for the help.

I was asking, because example 1 essentially says (!(isset(v1=1) || (isset(v2=2)).

I was wondering whether the ! in the first example applies to both v1 and v2, or whether, i'd have to use the syntax in example 2

(!(isset(v1=1)) || !(isset(v2=2))).

Thanks,

Matthew Millar

Re: IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Sun Oct 11, 2009 7:46 am
by Mark Baker
mattmill30 wrote:(!(isset(v1=1) || (isset(v2=2))
Not very good way of showing an if, because it's executing variable setting as well, and I can't see how it's similar to example 1 in your original post.

(!(isset(v1=1) || (isset(v2=2))
sets v1 to a value of 1, result is true
isset(true) - no idea what will happen here

I'm trying to work out exactly what logic you want to implement

Re: IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Sun Oct 11, 2009 6:25 pm
by mattmill30
Hi Mark,

Appologies for the confusion, i've just re-read what i put, and even i don't understand what i meant :P it should've read like this:

if (!(isset(v1) || (isset(v2)))

I'm wondering if you could tell me whether the above uses the same logic as the following pseudo code:

IF (v1 isn't set) OR (v2 isn't set) THEN

or would i need two ! marks?

if (!(isset(v1)) || !(isset(v2))

Thanks for the patience, i think it must've been late when i posted yesterday.

Matthew Millar

Re: IF( OR OR OR) vs IF(( OR ) OR OR)

Posted: Sun Oct 11, 2009 7:21 pm
by califdon
mattmill30 wrote:if (!(isset(v1) || (isset(v2)))

I'm wondering if you could tell me whether the above uses the same logic as the following pseudo code:

IF (v1 isn't set) OR (v2 isn't set) THEN

or would i need two ! marks?

if (!(isset(v1)) || !(isset(v2))

Thanks for the patience, i think it must've been late when i posted yesterday.
No, the negation operator (!) applies to whatever single value follows it. Can you imagine how confusing it would be if it continued to apply to other values? If you want both conditions to be negated, you must either use 2 ! operators or enclose whatever you want in parentheses, but notice that then the logic is entirely different!

Code: Select all

if(!isset(v1) || !isset(v2))
-- means "if v1 is not set OR if v2 is not set".

Code: Select all

if (!(isset(v1) || isset(v2)))
-- means "if it's NOT true that either v1 or v2 IS set", which is the same as saying that the only way for the IF condition to be true is if EITHER v1 OR v2 OR BOTH are set.