![]() |
PHP error handling
Hi,
Does anyone know how I can make a php script ignore an error? I have a script that returns values from an xml file. If one of the tags doesn't exist the script fails halfway through and prints an error to the screen. I would like it just to keep going and ignore the error. I'm using php 4 so I can't use try-catch blocks, as far as I know they are only in php5. Is that possible? Thanks, Paddy |
It depends upon the type of errors that you are getting but it sounds as if you want to use the error_reporting() function to determine which errors you want to report.
A second approach is the use of the prefix operator @ which stops the reporting of errors (although I personally feel that using @ is a bad habit, it does have its uses) |
Thanks for the info, the @operator stops the errors being reported to the screen but the script execution stills stops at that point.
Paddy |
What happens when you use error_report()?
What are the errors that you receive, from which function? |
error_reporting(0) does indeed suppress the error but the program execution fails at that point.
Here is my code, I have an array of items <Item></Item> in my xml file, must of them have a tag <ItemModel></ItemModel> but some of them don't. So when I try and get the value of the ITemModel tag for items that don't have an ItemModel I get the following error. Code:
Fatal error: Call to a member function on a non-object in Test.php on line 90Code:
error_reporting(0); |
Okay well you need to determine if $test[] really is an object before you try and dereference it. Add a conditional statement after the line that assigns $test to see if it has been set up correctly. If there are no tags then you want get_elements_by_tagname() to return an empty array and then you can look at the size of the array (if it is > 0) then continue with the next line of code:
PHP Code:
|
Thanks that works perfectly! I should have tried to approach it that way from the start instead of trying to ignore the errors!
Paddy |
| All times are GMT -5. The time now is 12:53 AM. |