GTS Joe GTS Joe - 1 month ago 8
PHP Question

die Function Not Triggering

In the following code, the die() function should execute since the URL I pass to simplexml_load_file() is bad. simplexml_load_file() returns FALSE, which should trigger die():

$url = 'http://www.badurl.com';

$xml = simplexml_load_file($url) or die('Error: Can\'t create the object.');


How come instead I'm getting the following error message?

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.

Answer

You can use the @ operator if you want silence the warning, or disable display_errors.

$xml = @simplexml_load_file($url) or die('Error: Can\'t create the object.');

simplexml_load_file returns false with or without the warning, and die is executed anyway.