我需要一種方法來檢測'foreach'是否不成功。如果是失敗,則重複出現在當前'其他'中的相同錯誤消息。測試如果一個Foreach成功
<?php
if(file_exists('redirects.xml')) {
$xml = simplexml_load_file('redirects.xml');
if(isset($_GET['r']) && $_GET['r'] != '' && !is_array($_GET['r'])) {
foreach($xml->short as $shorts) {
if($shorts->name == $_GET['r']) {
header('Location: '.$shorts->url);
break;
}
}
}
else {
header("refresh:2;url=http://www.wlatw.co/");
echo '<div align="center" style="font-weight: bolder; font-size: 24px;">Malformed URL</div><div align="center" style="font-weight: bolder; font-size: 16px;">Redirecting...</div>';
}
}
?>
是什麼讓'foreach'環成功還是不成功?你的意思是說如果沒有'$ xml-> short'作爲非成功的迭代? – 2013-02-20 03:45:45
你應該總是在'header('Location:...')'header後終止腳本。例如使用'die;'。 – 2013-02-20 03:46:05