dongtaidai0492 2017-11-10 06:43
浏览 50
已采纳

注意:在函数内部检查时未定义索引

Here is the function:

function is_set($var, $placeholder = null){
    if(isset($var)){
        return $var;
    } else {
        return $placeholder;
    }
}

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
    is_set($_POST['freq'], '');
}

It returns "Notice: Undefined index: freq in... "

While this code works well:

echo isset($_POST['freq']) ? $_POST['freq'] : '';

Why is that??

  • 写回答

1条回答 默认 最新

  • dqzpt40064 2017-11-10 07:09
    关注

    First print_r($_POST);and check variable you trying to access is available. you are tryin g to pass $_POST['freq']) for the validation before checking whether the variable exists. exception triggers when your execution hits is_set($_POST['freq']); without poast parameter 'freq' . Try some thing like

        if(!empty($_POST['freq'])){
            is_set($_POST['freq']);
        }
    

    or pass whole $_POST to is_set function and validate variable there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?