Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post

    != not working with multiple arguments

    Ok.... I'm trying to filter out some lines that will be returned to me in an email, so I am doing this:

    PHP Code:
    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:

    PHP Code:
    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?

  • #2
    New to the CF scene
    Join Date
    Sep 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Change the OR to && (or AND)
    PHP Code:
     if ($key != "fm_emailid" AND $key != "subject") {

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



  • #3
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    Hmm... dunno how that works..... how can it equal both values @ once?

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

    PHP Code:
    if ($key != "fm_emailid") {

        if (
    $key != "subject") {

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

        }



  • #4
    Regular Coder
    Join Date
    Apr 2004
    Location
    Birmingham, MI
    Posts
    131
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    PHP Code:
    if ( ($key != "fm_emailid") && ($key != "subject")) {
     
    // do this


  • #5
    Regular Coder
    Join Date
    Aug 2004
    Location
    The US of A
    Posts
    767
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #6
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #7
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    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

  • #8
    4xz
    4xz is offline
    Regular Coder
    Join Date
    Aug 2004
    Location
    localhost
    Posts
    163
    Thanks
    0
    Thanked 1 Time in 1 Post
    if($key != "fm_emailid" or $key != "subject")

    Will always succeed. That's your problem.

  • #9
    Regular Coder trib4lmaniac's Avatar
    Join Date
    Feb 2004
    Location
    Cornwall, UK
    Posts
    535
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if $key == "subject" then it won't equal "fm_emailid" (and visa-versa) so the whole if statment will always evaluate to true.

  • #10
    New to the CF scene
    Join Date
    Mar 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by firepages View Post
    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.

  • #11
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by cliffclof View Post
    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #12
    Regular Coder
    Join Date
    Apr 2004
    Posts
    682
    Thanks
    24
    Thanked 1 Time in 1 Post
    6 years on & I find this post funny..... I can now easily see the problem I was having.

  • #13
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Quote Originally Posted by cyphix View Post
    6 years on & I find this post funny..... I can now easily see the problem I was having.
    Thats always the case hah!
    This is approaching our record for digs, I think our oldest dig was just over 8 years. Oh well.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •