shortened php if else block

后端 未结 2 1603
小鲜肉
小鲜肉 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: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.)

提交回复
热议问题