在 for 循环中重新分配循环变量.错误


Reassign loop variable inside for loop. Bug?

我试图理解为什么这段代码会终止(是的,我测试过它) - https://github.com/drkyro/mmcFE-litecoin/blob/master/cronjobs/cronjob.php

34: for($i = 0; $i < $numAccounts; $i++){
.
.
.
63:             $i=0;
.
.
.
129: }

这个简单的测试永无止境:

for($i = 0; $i < 10; $i++){
    echo "i1 = $i";
    $i=0;
}

有什么区别,为什么在第一种情况下不重新分配循环变量?

因为$i = 0并不总是执行。仅当帐户不存在时,才会执行它。然后创建该帐户,因此在下一次传递时$i不会重置为 0 。您还需要代码周围的上下文。

此行为的简化视图:

for($i = 0; $i < $numAccounts; $i++) {
    $accountExistsQ = mysql_query("SELECT id FROM networkBlocks WHERE accountAddress = '".$transactions[$i]["txid"]."' ORDER BY blockNumber DESC LIMIT 0,1")or die(mysql_error());
    $accountExists = mysql_num_rows($accountExistsQ);
    if(!$accountExists) {
      mysql_query("INSERT INTO `networkBlocks` (`blockNumber`, `timestamp`, `accountAddress`, `confirms`, `difficulty`) ".
        "VALUES ('$assoc_block', '$assoc_timestamp', '" .$transactions[$i]["txid"]. "', '" .$transactions[$i]["confirmations"]. "', '$difficulty')");
      $i=0;
    }
  }
}