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

前端 未结 13 1774
抹茶落季
抹茶落季 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:07

    As mellowsoon suggest, you might consider this approach:

    required = array('myvar' => "defaultValue1", 'foo' => "value2", 'bar' => "value3", 'baz' => "value4");
    $missing = array_diff($required, array_keys($_GET));
    foreach($missing as $key => $default  ) {
        $_GET[$key] = $default  ;
    }
    

    You put the default values and set the not recieved parameters to a default value :)

提交回复
热议问题