1

每当我尝试运行这段代码时,它似乎进入了一个无限循环。但是,我无法弄清楚是什么导致了这个问题。也许对这件事多加注意就能指出问题所在?

该问题仅在存在不同年份区域时才会出现,例如 2012-2018

示例:$this->budget_model->set_monthly_budget('1','2012','8','2014','1');

    function set_monthly_budget($start_month, $start_year, $end_month, $end_year, $budget_group_id)
{

    $user_id = 2;

    // Current date
    $current_month = $start_month;
    $current_year = $start_year;
    $days_in_current_month = cal_days_in_month(CAL_GREGORIAN, $current_month, $current_year);
    $company_id = 1;
    $month_goal = 100;

    // Check if it is the current month
    if($start_year == $end_year)
    {

        for($x=$current_month;$x<=$end_month;$x++)
        {

            $data = array(
                'user_id' => $user_id,
                'budget_group_id' => $budget_group_id,
                'month' => $x,
                'year' => $start_year,
                'company_id' => $company_id,
                'month_goal' => $month_goal
            );

            // Inserting information into the database
            $this->db->insert('budget_month',$data);

        }

        return true; // Return true if the task was completed

    }

    if($start_year !== $end_year)
    {

        $temp_start_year = $start_year;

        while($temp_start_year !== $end_year)
        {

            // Check if we are in the same year as we started
            if($temp_start_year == $current_year)
            {

                // Insert remaining months for this year
                for($x=$current_month;$x<=12;$x++)
                {

                    $data = array(
                        'user_id' => $user_id,
                        'budget_group_id' => $budget_group_id,
                        'month' => $x,
                        'year' => $temp_start_year,
                        'company_id' => $company_id,
                        'month_goal' => $month_goal
                    );

                    // Inserting information into the database
                    $this->db->insert('budget_month',$data);

                }

            }

            // Check if the temp and end year is the same
            if($temp_start_year < $end_year)
            {

                // Insert remaining months for this year
                for($x=1;$x<=12;$x++)
                {

                    $data = array(
                        'user_id' => $user_id,
                        'budget_group_id' => $budget_group_id,
                        'month' => $x,
                        'year' => $temp_start_year,
                        'company_id' => $company_id,
                        'month_goal' => $month_goal
                    );

                    // Inserting information into the database
                    $this->db->insert('budget_month',$data);

                }

            }

            // Check if we are in the same year as we started

            if($temp_start_year == $end_year)
            {

                // Insert remaining months for this year
                for($x=1;$x<=$end_month;$x++)
                {

                    $data = array(
                        'user_id' => $user_id,
                        'budget_group_id' => $budget_group_id,
                        'month' => $x,
                        'year' => $temp_start_year,
                        'company_id' => $company_id,
                        'month_goal' => $month_goal
                    );

                    // Inserting information into the database
                    $this->db->insert('budget_month',$data);

                }

            }

            $temp_start_year++;

        }

    }

}
4

1 回答 1

6

在你的代码中

while($temp_start_year !== $end_year)

您使用了 !== 它还检查了 2 个变量的类型是否相同。

但是这条线

$temp_start_year++;

会将变量隐式转换为整数。

因此 !== 将整数与字符串进行比较,这将始终评估为真。

解决方案就像使用 != 而不是 !== 一样简单,或者在调用函数时输入整数而不是字符串(删除单引号)。

于 2012-11-28T07:25:55.433 回答