如果出现任何错误,我将使用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