繁体   English   中英

PHP无限循环

[英]Infinite loop on while PHP

首先,我不是程序员(只是具有一些PHP知识)。 我制作了一个带有事件列表的网站,其中包含一个选择字段,用于过滤每月加载Ajax的事件。

该代码完美运行了大约3个月。 但突然在5天前,它停止工作并开始生成无限循环。

我调试了代码,发现问题是“ while”循环。 我试图了解为什么这种方法在一段时间内能完美运行,然后却无法实现。 (使服务器死循环)。

此功能仅给出下个月的第一天。 一切正常。

function first_day_next_month($month, $year){

  $month = str_replace("0", "", $month);

  if ($month == 12){
    return ($year+1)."0101";
  }else{
    return $year.str_pad($month+1, 2, "0", STR_PAD_LEFT )."01";
  }

}

这一个为选择字段生成“选项”:

function ddown(){

  $start = strtotime("10 December 2017");

  $end = strtotime("+3 months");

  $current = $start;

  $meses = array(

    "Jan" => "enero",
    "Feb" => "febrero",
    "Mar" => "marzo",
    "Apr" => "abril",
    "May" => "mayo",
    "Jun" => "junio",
    "Jul" => "julio",
    "Aug" => "agosto",
    "Sep" => "septiembre",
    "Oct" => "octubre",
    "Nov" => "noviembre",
    "Dec" => "diciembre"

    );
  $o = "";

  $selected = "";
  $cls = " class=\"gray-out\"";
  while ($current < $end){

    if(date("m", $current) == date("m", strtotime("today")) AND date("Y", $current) == date("Y", strtotime("today"))){
      $selected = " selected=\"selected\"";
      $cls = "";
    }else{
      $selected = "";
    }



    $current = strtotime(first_day_next_month( date("m", $current), date("Y", $current) ));
  }

  return $o;

}

我发现这段代码现在正在生成无限循环:

while ($current < $end){

    if(date("m", $current) == date("m", strtotime("today")) AND date("Y", $current) == date("Y", strtotime("today"))){
      $selected = " selected=\"selected\"";
      $cls = "";
    }else{
      $selected = "";
    }



    $current = strtotime(first_day_next_month( date("m", $current), date("Y", $current) ));
  }

$ current变量不会在while上更新。 据我了解,这应该将2017年12月10日推迟到今天+3个月,并且在这些日期之间的每个月都要输出和“期权”。

关于如何解决这个问题的任何建议?

(对不起我的英语不好)。

谢谢!

在first_day_next_month函数中,您具有:

$month = str_replace("0", "", $month);

这将用1替换10,它将在每年的10月恢复到1月。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM