2010-01-09 23 views
0

我正在使用domdocument加載來獲取一些數據。有時,這些數據不可用,當腳本運行時,我會收到錯誤或警告。我注意到如果數據不存在,我可以測試返回值。最好使用while循環還是if語句?domdocument加載錯誤。如何測試

+0

示例代碼流? – Matchu 2010-01-09 18:33:15

回答

0

我是假設您正試圖加載遠程文檔,這就是爲什麼它偶爾不可用。 我建議您嘗試以下操作:

<?php 
$dom = new DOMDocument(); 

$tries = 0; 
$retryLimit = 10; // # of times to try loading 
$interval = 2; // wait time between attempts (seconds); 
while (!$dom->load('http://www.example.com/')) { 
    if (++$tries > $retryLimit) { 
     throw new Exception("Unable to load remote document"); 
    } 
    sleep($interval); 
} 

這也可以寫成的,當然循環。這並不重要。

+0

謝謝你hobodave。這正是我正在尋找的構造。 – BiBo 2010-01-09 18:50:43

+0

嘿哈博...我可以問你一個關於這個問題嗎?我從來沒有見過以加號爲前綴的變量。那是什麼? – BiBo 2010-01-09 18:52:31

+0

BiBo:http://www.php.net/manual/en/language.operators.increment.php – hobodave 2010-01-09 18:56:04