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条)

报告相同问题?

悬赏问题

  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历