duanpuchen3142 2015-11-18 11:22
浏览 186
已采纳

为什么PHP告诉我这个变量没有定义?

I'm trying to filter some logs like I need them and tried to it dynamic. I have some domains and I'm trying to filter some things from it, and it all works like I want it - but now I changed the domain name and now my code doesn't work anymore. It says one variable isn't defined.

      $sp_bots = shell_exec("grep bot | awk '{print $12}' /var/www/laravel/logs/vhosts/zahnmedizinisches-fachzentrum-berlin.de.log");
    $array_sp_bots = explode("
", $sp_bots);
    $all_bots = array();
    foreach($array_sp_bots as $bots){
        if(strpos($bots, "bot")){
            $all_bots[] = $bots;
        }
    }
    # count values of strings in array
    if (!empty( $all_bots )) {
        $bots = array_count_values($all_bots);
        arsort($bots);
        $mostOccuring = max(array_count_values($all_bots));
        $bot_keys = array_keys($bots);
        #number of total bots
        $count_bots = count($all_bots);
    }

and in my returns:

return view('/domains/data', [

       'count_bots' => $count_bots,
        'bot_keys' => $bot_keys,
        'mostOccuring' => $mostOccuring,
    ]);

but all three variables in my return are undefined.. anybody knows why?

  • 写回答

2条回答 默认 最新

  • duanpo6079 2015-11-18 11:25
    关注

    You have to initialize the array as an empty array before the loop:

    $all_bots = array();          //init the empty array
    
    foreach($array_sp_bots as $bots)
    {
        if(strpos($bots, "bot"))
        {
            $all_bots[] = $bots;  //here you can add elements to the array
        }
    }
    

    in your case, if the loop does not execute at least one time, the variable $all_bots will be undefined

    EDIT

    After the loop, to handle the case when the array is empty do somthing like this:

    //if there is some element in all_bots...
    if ( ! empty( $all_bots ) )
    {
        # count values of strings in array
        $bots = array_count_values($all_bots);
        arsort($bots);
        $mostOccuring = max(array_count_values($all_bots));
        $bot_keys = array_keys($bots);
        #number of total bots
        $count_bots = count($all_bots);
    }
    //handle the case the variable all_bots is empty
    else
    {
        $bots = 0;
        $count_bots = 0;
        $bot_keys = 0;
        $mostOccuring = 0;
    }
    

    EDIT2

    You have the variables undefined in your return because when all $all_bots is empty they are not set. Check my edit above, i have added them to the if statement. But you have to handle this case in your application according to your needs, think this way: What these variables should contain when $all_bots is empty? Then assign the values to the variables in the if statement

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集