如果出現任何錯誤,我將使用goto
,並且它會從頂部重新開始。把goto換成while循環?
我也不能在函數中使用goto
來跳出函數。
我知道goto
是壞的,但while
循環如何適合我的代碼?
例如:
$ch = curl_init();
retry:
$result = curlPost($ch, "home.php", http_build_query($arg));
if (strpos($postResult, "Welcome") !== false) {
sleep(10);
goto retry;
}
$sql = "SELECT * FROM data";
$q = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_assoc($q)) {
$name['something'] = $row['name'];
submitData($ch,$name);
}
function submitData($ch,$name) {
$result2 = curlPost($ch, "submit.php", http_build_query($name));
if (strpos($postResult, "Website close") !== false) {
sleep(10);
goto retry; //goto wouldnt work.
}
// do something here
}
它會適合比goto更好。 – Phantom
@Phantom在我的示例中使用while循環回答問題。 – user1246800