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

前端 未结 13 1613
抹茶落季
抹茶落季 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:31
    <?php
    
    function myset(&$var,$value=false){
        if(isset($var)):
            return $var == $value ? $value : false;
        endif;
        return false;
    }
    
    $array['key'] = 'foo';
    
    var_dump(myset($array['key'],'bar')); //bool(false)
    
    var_dump(myset($array['key'],'foo'));//string(3) "foo"
    
    var_dump(myset($array['baz'],'bar'));//bool(false) 
    
    0 讨论(0)
提交回复
热议问题