用于检查自循环 PHP 开始以来变量是否更改的算法


//Initialize
i=0;
foreach(runs 8 times) {
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }
  if(what condition do i put here to check if $i changed from the start of the loop?) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

嘿伙计们,我确定这是显而易见的,但我很难使用这个算法,每次我都需要确保$i与循环开始时不同,并根据这种区别执行操作 1 或操作 2。

对于迭代 1,我可以输入 if($i == 0) {,但随后在迭代 2 和 3 上可能会失败。

您应该能够使用另一个变量,并在该变量中保存$i的值(如果它已更改)。然后,当您检查更改时,您始终可以将该变量与$i进行比较。

不过,我认为您不应该在循环中拥有$i。

$previousValue = null;
$i=0;
foreach(runs 8 times) {
  //Initialize
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }
  if($previousValue == $i) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
    $previousValue = $i;
  }
}

只需记住开头的状态,然后检查:

foreach(runs 8 times) {
  //Initialize
  i=0;
  oldi = i;
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }
  if(i != oldi) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

试试这个:

$previous = 0;
foreach(runs 8 times) {
//Initialize
$i = 0;
if(some condition that sometimes happens) {
    $i = $i + 3;
} else if (some other condition that sometimes happens) {
//Do nothing with i
} else if(some condition that sometimes happens) {
    $i = $i -4;
}

if($i != $previous) {
    //nothing changed, do action 1
} else {
    //Else something changed and do action 2.  
}
$previous = $i;
}
$ichanged = false;
i=0;
foreach(runs 8 times) {
  //Initialize
  if(some condition that sometimes happens) {
    $ichanged = true;
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $ichanged = true;
    $i = $i -4;
  }
  if($ichanged == false) {
    //nothing changed, do action 1        
  } else {
    //Else something changed and do action 2.  
    $ichanged = false;//RESET for next iteration
  }
}
很简单

,只需在循环中再添加一个变量,并在每次循环迭代时将其初始化为i

试试这个代码:

//Initialize
i=0;
j=0;
foreach(runs 8 times) {
  j=i;
  if(some condition that sometimes happens) {
    $i = $i + 3;
  } else if (some other condition that sometimes happens) {
    //Do nothing with i
  } else if(some condition that sometimes happens) {
    $i = $i -4;
  }
  if(j==i) {
    //nothing changed, do action 1
  } else {
    //Else something changed and do action 2.  
  }
}

既然只会评估其中一种情况,为什么不直接调用在$i立即更改时执行某些操作的函数呢?

$i = 0;
foreach(/*runs 8 times*/) {
  if(/*some condition that sometimes happens*/) {
    iChanged($i, $i + 3);
    $i -= 3;
  } elseif (/*some other condition that sometimes happens*/) {
    iDidntChange($i);
  } elseif(/*some condition that sometimes happens*/) {
    iChanged($i, $i - 4);
    $i -= 4;
  } else {
    iDidntChange($i);
  }
}

相关内容

  • 没有找到相关文章

最新更新