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 win2012 iscsi ipsec
  • ¥15 封装的 matplotlib animation 不显示图像
  • ¥15 python摄像头画面无法显示
  • ¥15 关于#3d#的问题:d标定算法(语言-python)
  • ¥15 cve,cnnvd漏洞扫描工具推荐
  • ¥15 图像超分real-esrgan网络自己训练模型遇到问题
  • ¥15 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)