use of '!' with '=='

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
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

use of '!' with '=='

Post 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
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Code: Select all

elseif ($_POST['1'] == 'x' && $_POST['2'] == 'y' && ($_POST['3'] != 'z' && $_POST['3'] != 'a')
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

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

Post 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 "!=="
phpnoobyyy
Forum Newbie
Posts: 14
Joined: Mon Oct 12, 2009 3:27 pm

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

Post by phpnoobyyy »

Great, it works!

Thanks for your help.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

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

Post by jackpf »

!= means not equivalent to. !== means not the same as.

Basically, === compares data types as well.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

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

Post 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 ===.
User avatar
Grizzzzzzzzzz
Forum Contributor
Posts: 125
Joined: Wed Sep 02, 2009 8:51 am

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

Post 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
Post Reply