Jump to content

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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.