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.

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

报告相同问题?

悬赏问题

  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决
  • ¥15 pycharm输出和导师的一样,但是标红