Page 1 of 1

use of '!' with '=='

Posted: Mon Nov 30, 2009 2:39 pm
by phpnoobyyy
Hi guys

I have the following code:

Code: Select all

elseif ($_POST['1'] == x and $_POST['2'] == y and $_POST['3'] !== z or a)
    {
    $errors[] = 'Error';
    
    }
It isn't functioning how I want it to. I want it to come up with an error message if post 3 does not equal 'z' or 'a'. However, it comes up with the error message regardless of whether post 3 is 'z' or 'a'. I've been trying to figure out where the problem might be myself, but without success.

Any help as always would be highly appreciated.

Many thanks

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 3:06 pm
by AbraCadaver

Code: Select all

elseif ($_POST['1'] == 'x' && $_POST['2'] == 'y' && ($_POST['3'] != 'z' && $_POST['3'] != 'a')

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 3:09 pm
by Grizzzzzzzzzz
phpnoobyyy wrote:Hi guys

I have the following code:

Code: Select all

elseif ($_POST['1'] == x and $_POST['2'] == y and $_POST['3'] !== z or a)
    {
    $errors[] = 'Error';
    
    }
It isn't functioning how I want it to. I want it to come up with an error message if post 3 does not equal 'z' or 'a'. However, it comes up with the error message regardless of whether post 3 is 'z' or 'a'. I've been trying to figure out where the problem might be myself, but without success.

Any help as always would be highly appreciated.

Many thanks
yep, as the guy posted above you can't use 'or' and 'and', or becomes '||' and and becomes '&&'

and it's "!=" not "!=="

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 3:14 pm
by phpnoobyyy
Great, it works!

Thanks for your help.

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 3:54 pm
by jackpf
!= means not equivalent to. !== means not the same as.

Basically, === compares data types as well.

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 4:07 pm
by jayshields
Grizzzzzzzzzz wrote: yep, as the guy posted above you can't use 'or' and 'and', or becomes '||' and and becomes '&&'

and it's "!=" not "!=="
Both incorrect statements.

You can use || or OR, just like you can use && and AND. Although be careful with precedence (they differ if I remember correctly). [EDIT: Yep, http://uk3.php.net/manual/en/language.o ... ogical.php]

!= and !== are two different things, as jackpf statement they are the negated equivilent of == and ===.

Re: use of '!' with '=='

Posted: Mon Nov 30, 2009 6:18 pm
by Grizzzzzzzzzz
jayshields wrote:
Grizzzzzzzzzz wrote: yep, as the guy posted above you can't use 'or' and 'and', or becomes '||' and and becomes '&&'

and it's "!=" not "!=="
Both incorrect statements.

You can use || or OR, just like you can use && and AND. Although be careful with precedence (they differ if I remember correctly). [EDIT: Yep, http://uk3.php.net/manual/en/language.o ... ogical.php]

!= and !== are two different things, as jackpf statement they are the negated equivilent of == and ===.
My response regarding != was in context to the guy's problem.

Although i personally didn't know you could still use AND/OR, so cheers for that clarification