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 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用