2010-01-09 47 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