Jump to content

[SOLVED] I've lost my mind over a simple one line conditional


kratsg

Recommended Posts

I'm taking an input from the URL $_GET['do'] which contains one of two values: "approve" or "deny"

 

If $_GET['do'] is set to a value that is not either one of these, I'll have it display an error, so I wrote the following conditional:

 

if($_GET['do'] != 'approve' || $_GET['do'] != 'deny')
//display error (which also ends the script with a die function

//else continue along

 

However, $_GET['do'] as either 'approve' or 'deny' seems to trigger this error, so I did a var_dump

 

var_dump(($_GET['do'] == 'approve' || $_GET['do'] == 'deny'));

 

This returned true if I picked either approve or deny, but false for all others. So I've determined that it has something to do with the "!=" portion... yet for some reason, I can't imagine what's wrong with that... seeing how it should work fine. Any ideas or shall I have to do the if(!($_GET['do'] == 'approve' || $_GET['do'] == 'deny')) instead?

I would look at triming $_GET['do']. Sometimes you get spaces through post / get.

 

Done that, even hard-coded $_GET['do'] into the script and then checked the output... maybe I've stared at the code for too long?

 

var_dump($_GET['do']) in all cases returns string(7) "approve" or string(4) "deny"

var_dump("approve") returns string(7) "approve"

var_dump("deny") returns string(4) "deny"

 

I must be missing something E_E

its because that if statement will always run true. You have to test if it is not equal to one AND not equal to the other, not if its not equal to one OR the other

 

if($_GET['do'] != 'approve' && $_GET['do'] != 'deny')

 

with the way you had it, if its approve, the first test will be false, but the second true, and vice versa for deny

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.