...

View Full Version : != not working with multiple arguments



cyphix
10-09-2004, 05:31 PM
Ok.... I'm trying to filter out some lines that will be returned to me in an email, so I am doing this:



if ($key != "fm_emailid") {

$datastr = $datastr . "$key: $keyval\r\n";

}


That works fine.... "fm_emailid" is not returned; however, if I add multiple arguments it doesn't work:



if ($key != "fm_emailid" or $key != "subject") {

$datastr = $datastr . "$key: $keyval\r\n";

}


Using this I get BOTH "fm_emailid" & "subject" returned when I shouldn't get either.

Any ideas?

StPaul24
10-09-2004, 10:10 PM
Change the OR to && (or AND)

if ($key != "fm_emailid" AND $key != "subject") {

$datastr = $datastr . "$key: $keyval\r\n";

}

cyphix
10-10-2004, 08:11 AM
Hmm... dunno how that works..... how can it equal both values @ once?

Anyway... I've just done it like this now & it works good:



if ($key != "fm_emailid") {

if ($key != "subject") {

$datastr = $datastr . "$key: $keyval\r\n";

}

}

circusbred
10-11-2004, 01:47 AM
Your original argument stated that if $key equaled either of the two, to go ahead with the statement. So if one of those statements held true, the values would return.

You new code is essentially saying the same thing as:

if ( ($key != "fm_emailid") && ($key != "subject")) {
// do this
}

Kurashu
10-11-2004, 04:40 AM
That doesn't make any sense though. != means not equal to, and PHP should parse it as IF $key DOESN'T EQUAL fm_emailid OR subject THEN ...dadadada. But, I tested it and it doesn't. How come the A&&/AND statement works?

This is all to confusing. @_@ Maybe it is that I am tired?

firepages
10-11-2004, 09:25 AM
PHP is lazy in reading conditionals and reads from left to right , e.g ...

$yaks = 'gurgle';
if( $yaks != 'blah' &&... at this point PHP gives up since the first argument fails and the next condition is AND (&&) ( e.g. its going to fail anyway so why bother )

if( $yaks != 'blah' || ... PHP sees the OR (||) and decides its worth carrying on.


how can it equal both values @ once?
it can't , & thats the point ;)

cyphix
10-11-2004, 09:34 AM
if( $yaks != 'blah' || ... PHP sees the OR (||) and decides its worth carrying on.

If that's the case why didn't my first example work :confused:

4xz
10-11-2004, 12:52 PM
if($key != "fm_emailid" or $key != "subject")

Will always succeed. That's your problem.

trib4lmaniac
10-11-2004, 08:08 PM
if $key == "subject" then it won't equal "fm_emailid" (and visa-versa) so the whole if statment will always evaluate to true.

cliffclof
03-26-2010, 09:38 AM
PHP is lazy in reading conditionals and reads from left to right , e.g ...

$yaks = 'gurgle';
if( $yaks != 'blah' &&... at this point PHP gives up since the first argument fails and the next condition is AND (&&) ( e.g. its going to fail anyway so why bother )

if( $yaks != 'blah' || ... PHP sees the OR (||) and decides its worth carrying on.


it can't , & thats the point ;)


This post is going to be 10 years old soon. Sorry. But maybe this would help someone since this is high in Google.

I think he meant:

$yaks = 'gurgle';
if( $yaks != 'blah' ||... at this point PHP gives up since the first argument fails and the next condition is OR (||) ( e.g. its going to fail anyway so why bother )

if( $yaks != 'blah' && ... PHP sees the AND (&&) and decides its worth carrying on.

Fou-Lu
03-26-2010, 05:00 PM
This post is going to be 10 years old soon. Sorry. But maybe this would help someone since this is high in Google.

I think he meant:

$yaks = 'gurgle';
if( $yaks != 'blah' ||... at this point PHP gives up since the first argument fails and the next condition is OR (||) ( e.g. its going to fail anyway so why bother )

if( $yaks != 'blah' && ... PHP sees the AND (&&) and decides its worth carrying on.

That is not correct; Firepages was correct. When using an ||, PHP will evaluate every option up to true to ensure that its not false. if (false || true) is always true, it sees false as its first option and continues processing to ensure that the remaining are also false (which it is not in this example). Once a true is found, it stops processing at that point since any combination will still result in true.

On the other hand, && evaluates up to only a false value: if (false && true) will only see up to the first evaluation and then give up since it can never be true at this point; every combination will still result in false.

These evaluations can be altered based on the use of operator precedence though (&& versus AND and || versus OR), but the logic still applys into the groupings of such precedences.

You can see this behaviour between bitwise operations if 0 = false and 1 = true:
0 & 1 = 0. This is (sorta) the same as false && true. So we know its false.
0 | 1 = 1. This is (sorta) the same as false || true. So we know its true.

The overall idea is:
When using ||, continue to evaluate while false.
When using &&, continue to evaluate while true.

So in the case of an item that is first evaluated as false, the || will continue until it either matches a true or until it fails. The && will immediately be false.

cyphix
03-26-2010, 11:08 PM
6 years on & I find this post funny..... I can now easily see the problem I was having. :p

Fou-Lu
03-26-2010, 11:12 PM
6 years on & I find this post funny..... I can now easily see the problem I was having. :p

Thats always the case hah!
This is approaching our record for digs, I think our oldest dig was just over 8 years. Oh well.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum