PHP中的无止境循环
每当我试图运行这段代码时,它似乎进入了一个无休止的循环。但是,我不知道是什么导致了这个问题。也许多看一眼就能指出问题所在 只有当存在不同的年份区域时才会出现问题,例如2012-2018年 示例:$this->budget_model->set_monthly_budget'1'、'2012'、'8'、'2014'、'1'PHP中的无止境循环,php,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, $budge
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++;
}
}
}
在代码中
while($temp_start_year !== $end_year)
你用过!==这也会检查两个变量的类型是否相同
但是这条线
$temp_start_year++;
将隐式地将变量转换为整数
因此!==将整数与字符串进行比较,字符串的计算结果始终为true
解决方案与使用一样简单!=而不是!==,或者在调用函数时输入一个整数而不是字符串,删除代码中的单引号。
while($temp_start_year !== $end_year)
你用过!==这也会检查两个变量的类型是否相同
但是这条线
$temp_start_year++;
将隐式地将变量转换为整数
因此!==将整数与字符串进行比较,字符串的计算结果始终为true
解决方案与使用一样简单!=而不是!==,或者在调用函数时输入一个整数而不是字符串,删除单引号。如果$start\u year大于$end\u year,则while循环的条件将始终满足,形成一个无止境的循环。上面的代码有两个循环。使用一些echo语句缩小两个循环中没有终止的循环的范围。如果$start\u year大于$end\u year,while循环的条件将始终满足,形成一个无止境的循环。上面的代码有两个循环。使用一些echo语句缩小两个循环中哪一个没有终止的范围。