How to trap a PHP script error which dies (Perl has eval)?
I've got a script which works fine on our development server but dies on the clients server.
error_reporting(E_ALL);
if (function_exists('simplexml_load_file')) echo "function exists";
if (file_exists('test.xml'))
{
echo("<hr>just about to read local xml file :".__LINE__);
$xml = simplexml_load_file('test.xml'); // dies here
In Perl you can trap such errors with eval, is there anything 开发者_Python百科equivalent in PHP?
Depends on the manner in which the function fails.
In the case of simplexml_load_file()
, the error info is well documented
On errors, it will return FALSE.
and
Produces an E_WARNING error message for each error found in the XML data.
and
Tip
Use
libxml_use_internal_errors()
to suppress all XML errors, andlibxml_get_errors()
to iterate over them afterwards.
And, you can always set your own error handler or use the (troublesome) error suppression operator.
Sysadmin:- I've briefly turned on verbose error reporting and it's come back with this:
Fatal error: Cannot clone object of class SimpleXMLElement due to 'zend.ze1_compatibility_mode' in
http://archives.devshed.com/forums/php-108/simplexml-problem-982459.html
This configuration can be overridden on a per site basis with a .htaccess file directive http://www.activecollab.com/forums/topic/616/
this solved the problem. But why couldn't I turn on this level of error reporting from within my script?
精彩评论