PHP: Check if variable exist but also if has a value equal to something

前端 未结 13 1614
抹茶落季
抹茶落季 2020-12-01 13:46

I have (or not) a variable $_GET[\'myvar\'] coming from my query string and I want to check if this variable exists and also if the value corresponds to somethi

相关标签:
13条回答
  • 2020-12-01 14:23

    Well, you could get by with just if($_GET['myvar'] == 'something') since that condition presumes that the variable also exists. If it doesn't, the expression will also result in false.

    I think it's ok to do this inside conditional statements like above. No harm done really.

    0 讨论(0)
  • 2020-12-01 14:23

    No official reference but it worked when I tried this:

    if (isset($_GET['myvar']) == 'something')
    
    0 讨论(0)
  • 2020-12-01 14:26

    If you're looking for a one-liner to check the value of a variable you're not sure is set yet, this works:

    if ((isset($variable) ? $variable : null) == $value) { }
    

    The only possible downside is that if you're testing for true/false - null will be interpreted as equal to false.

    0 讨论(0)
  • 2020-12-01 14:27

    Sadly that's the only way to do it. But there are approaches for dealing with larger arrays. For instance something like this:

    $required = array('myvar', 'foo', 'bar', 'baz');
    $missing = array_diff($required, array_keys($_GET));
    

    The variable $missing now contains a list of values that are required, but missing from the $_GET array. You can use the $missing array to display a message to the visitor.

    Or you can use something like that:

    $required = array('myvar', 'foo', 'bar', 'baz');
    $missing = array_diff($required, array_keys($_GET));
    foreach($missing as $m ) {
        $_GET[$m] = null;
    }
    

    Now each required element at least has a default value. You can now use if($_GET['myvar'] == 'something') without worrying that the key isn't set.

    Update

    One other way to clean up the code would be using a function that checks if the value is set.

    function getValue($key) {
        if (!isset($_GET[$key])) {
            return false;
        }
        return $_GET[$key];
    }
    
    if (getValue('myvar') == 'something') {
        // Do something
    }
    
    0 讨论(0)
  • 2020-12-01 14:28

    My question is, exist any way to do this without declare the variable twice?

    No, there is no way to do this correctly without doing two checks. I hate it, too.

    One way to work around it would be to import all relevant GET variables at one central point into an array or object of some sort (Most MVC frameworks do this automatically) and setting all properties that are needed later. (Instead of accessing request variables across the code.)

    0 讨论(0)
  • 2020-12-01 14:31

    why not create a function for doing this, convert the variable your want to check into a real variable, ex.

    function _FX($name) { 
      if (isset($$name)) return $$name;
      else return null; 
    }
    

    then you do _FX('param') == '123', just a thought

    0 讨论(0)
提交回复
热议问题