LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   PHP error handling (http://www.linuxquestions.org/questions/programming-9/php-error-handling-414452/)

paddyjoy 02-12-2006 03:32 AM

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

graemef 02-12-2006 04:11 AM

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)

paddyjoy 02-12-2006 04:23 AM

Thanks for the info, the @operator stops the errors being reported to the screen but the script execution stills stops at that point.

Paddy

graemef 02-12-2006 04:25 AM

What happens when you use error_report()?

What are the errors that you receive, from which function?

paddyjoy 02-12-2006 05:03 AM

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 90
Code:

error_reporting(0);

foreach($responseDoc->get_elements_by_tagname('Item') as $item)
                {

                $test = $item->get_elements_by_tagname('ItemModel');
                $ItemModel = $test[0]->get_content();

}

Paddy

graemef 02-12-2006 06:14 AM

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:

foreach($responseDoc->get_elements_by_tagname('Item') as $item)
{
   
$test $item->get_elements_by_tagname('ItemModel');
   if (
count($test) > 0)
      
$ItemModel $test[0]->get_content();


You can also look at the is_a() or the more generic is_object() functions.

paddyjoy 02-12-2006 06:29 AM

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 05:27 PM.