0
  if(isset($_GET['a']) || isset($_GET['b']) || isset($_GET['c'])){
      if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x"){
          echo "YES";
      } else {
          echo "NO";
      }
  }

in this php code, i'm trying to check if one of those requests isset and if one of them value == 'x' or not, But the 2nd part if(($_GET['a'] || $_GET['b'] || $_GET['c']) == "x") doesn't work as intended at all, I wrapped it inside () hoping it would work, In this condition, do i have to separate it as i did inthe isset() part? or is there a better method to do that?

2 Answers 2

3

This is likely what you are looking for

UPDATE - I just changed || to && for the last condition in case you were quick to try it out.

if( (isset($_GET['a']) && $_GET['a'] == "x") || (isset($_GET['b']) && $_GET['b'] == "x") || (isset($_GET['c']) && $_GET['c'] == "x")){
      echo "YES";
} else {
      echo "NO";
}
Sign up to request clarification or add additional context in comments.

2 Comments

so this wrapping part ($_GET['a'] || $_GET['b'] || $_GET['c']) == "x" doesn't work as a collector for all variables as condition?
($_GET['a'] || $_GET['b'] || $_GET['c']) is saying: $_GET['a'] is TRUE or $_GET['b'] is TRUE or $_GET['c'] is TRUE. After evaluating this, you will have either TRUE if one of these is TRUE or FALSE if not. Then it compares whether this TRUE or FALSE value is equal to x. This is not what you wanted.
0

If you have to write a lot of conditionals you could use one of the following:

Using a foreach and a conditional:

$either_abc_is_x = function() {
    $keys = ['a','b','c'];
    foreach($keys as $key)
        if(isset($_GET[$key]) && $_GET[$key] == "x")
            return true;
    return false;
};

echo $either_abc_is_x() ? 'YES' : 'NO';

Using a an array filter with a conditional:

$get_abc_keys_equal_to_x = array_filter(['a','b','c'], function($v) {
  return isset($_GET[$v]) && $_GET[$v] == 'x';
});

echo $get_abc_keys_equal_to_x ? 'YES' : 'NO';

Array gymnastics:

$either_abc_is_x = isset($_GET) && in_array('x', array_intersect_key($_GET, array_flip(['a','b','c'])));

echo $either_abc_is_x ? 'YES' : 'NO';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.