shortened php if else block

后端 未结 2 1601
小鲜肉
小鲜肉 2020-12-22 06:12

what is the shorted if else block for this. I seen it somewhere before but cant remember it.

if (isset($_POST[\'value\')){
 $value = $_POST[\'value\'];
} els         


        
相关标签:
2条回答
  • 2020-12-22 06:48

    Are you referring to using the $_REQUEST global array instead of checking both $_POST and $_GET? If so, it should be:

    if(isset($_REQUEST['value']))
    {
        $value = $_REQUEST['value'];
    }else
        $value = '';
    

    Or the ternary form:

    $value = isset($_REQUEST['value']) ? $_REQUEST['value'] : '';
    
    0 讨论(0)
  • 2020-12-22 06:51
    $value = filter_input(FILTER_POST, 'value') ?: filter_input(FILTER_GET, 'value');
    

    Or if you have to get multiple variables, do this:

    $input = $_POST + $_GET + $defaults;
    $value = $input['value'];
    

    (The + operator does not override existing keys in the left array; it works like array_merge($defaults, $_GET, $_POST) in this case.)

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