在下面的代码中,die()函数应该执行,因为我传递给simplexml_load_file()的URL是坏的。simplexml_load_file()返回FALSE,这将触发die():
$url = 'http://www.badurl.com';
$xml = simplexml_load_file($url) or die('Error: Can''t create the object.');
为什么我得到以下错误信息?
Warning: simplexml_load_file(http://www.badurl.com): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /var/www/badurl.com on line 16
Warning: simplexml_load_file(): I/O warning : failed to load external entity "http://www.badurl.com" in /var/www/badurl.com on line 16
Error: Can't create the object.
如果您想静音警告或禁用display_errors
,则可以使用@
操作符。
$xml = @simplexml_load_file($url) or die('Error: Can''t create the object.');
simplexml_load_file
返回false
,无论是否有警告,die
仍然执行。