//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。算法檢查自從循環開始以來變量是否發生了變化PHP
對於迭代1,我可以把if($ i == 0){,但是在迭代2和3中有可能失敗。
添加另一個計數器,你也增加太多,不僅$ i? – bwoebi
因爲你有'i = 0;'作爲你的循環的第一行,你在哪裏增加'$ i'? – putvande
在這裏寫下你的確切代碼 –