在PHP中获得无限循环

时间:2012-11-28 07:21:59

标签: php

每当我尝试运行此代码时,它似乎进入无限循环。但是,我无法弄清楚是什么造成了这个问题。也许额外关注这件事能指出问题吗?

问题仅在存在不同年份区域时才会出现,例如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++;

        }

    }

}

1 个答案:

答案 0 :(得分:6)

代码

while($temp_start_year !== $end_year)

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

但这一行

$temp_start_year++;

将隐式地将变量强制转换为整数。

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

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