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

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??

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.