GTS Joe GTS Joe - 1 year ago 87
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 = '';

$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( failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in /var/www/ on line 16

Warning: simplexml_load_file(): I/O warning : failed to load external entity "" in /var/www/ on line 16
Error: Can't create the object.

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download