-3

I dont't understand this. If $_GET['action'] is set and action is not "login" or not "messages" I want to doAction().

My non working code:

if ( isset($_GET['action']) &&  ($_GET['action'] !== "login" || $_GET['action'] !== "messages") )     {
   doAction();
}

If I remove the || it works.

if ( isset($_GET['action']) &&  ($_GET['action'] !== "login") ) {
   doAction();
}

Any hints?

TheG
  • 19
  • 3

4 Answers4

0

You are using the "not equals" operator (!==). If you want to test if the value is login or messages, you should use the == operator:

if ( isset($_GET['action']) && 
     ($_GET['action'] == "login" || $_GET['action'] == "messages") ) {
   doAction();
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • Yeah, I had a typo in my original post. Sorry about that. Please see my edited post. – TheG Nov 22 '14 at 18:15
0
if ( isset($_GET['action']) && 
     ($_GET['action'] !== "login" || $_GET['action'] !== "messages") ) {
   doAction();
}
dadarya
  • 313
  • 1
  • 6
  • I used not equal operator because question is If $_GET['action'] is set and action is not "login" or not "messages" and in upper code condition is checking that action is set and action is login or message than do something.. – dadarya Nov 22 '14 at 18:21
  • So edit your answer to include that. I believe what @u_mulder was getting at is that a complete answer on StackOverflow should include both the fixed code and the explanation of how the fixed code works and/or why the code in the question didn't work. – ArtOfWarfare Nov 22 '14 at 18:48
0

When testing conditionals in PHP with ||, PHP will test the conditionals in the order they are written and if one of them is true, the whole conditional evaluates to true. This means that for your code if the value of $_GET['action'] is 'messages', the conditional will evaluate to true:

if ( isset($_GET['action']) &&  ($_GET['action'] !== "login" || $_GET['action'] !== "messages") ){
   doAction();
}

Because when the interpreter evaluates $_GET['action'] !== "login" and the value of $_GET['action'] is 'messages', the result of that evaluation is true and thus the code in the if block will be executed. I recommend you change your code to:

if ( isset($_GET['action']) &&  ($_GET['action'] !== "login" && $_GET['action'] !== "messages") ){
   doAction();
}

The || is changed into && and this means that the value of $_GET['action'] cannot be 'login' and it cannot be 'messages'.

Tom
  • 122
  • 2
  • 9
0

Okay, let's check execution of your if statement:

  1. suppose, action is some_action then isset($_GET['action']) true and $_GET['action'] !== "login" is true

As php has lazy execution (true and true is true) the comparison stopped and your doAction executes, fine, just what we need.

  1. suppose, action is login then isset($_GET['action']) true and $_GET['action'] !== "login" is false and $_GET['action'] !== "messages" is true (as login is definitely not messages). So (true and (false or true)) is true!

  2. finally suppose, action is messages then isset($_GET['action']) true and $_GET['action'] !== "login" is true. Considering lazy execution your doAction runs again.

Okay! Looks like that doAction will work always!

Fix for the problem is using and (&&) instead of or (||) in you if

if (isset($_GET['action']) 
    && ($_GET['action'] !== "login" && $_GET['action'] !== "messages") ) 
    // that should work properly!
u_mulder
  • 54,101
  • 5
  • 48
  • 64