LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2006, 03:32 AM   #1
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Rep: Reputation: 30
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
 
Old 02-12-2006, 04:11 AM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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)
 
Old 02-12-2006, 04:23 AM   #3
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks for the info, the @operator stops the errors being reported to the screen but the script execution stills stops at that point.

Paddy
 
Old 02-12-2006, 04:25 AM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
What happens when you use error_report()?

What are the errors that you receive, from which function?
 
Old 02-12-2006, 05:03 AM   #5
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
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
 
Old 02-12-2006, 06:14 AM   #6
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
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.
 
Old 02-12-2006, 06:29 AM   #7
paddyjoy
Member
 
Registered: Apr 2005
Location: Sydney
Distribution: FC5
Posts: 174

Original Poster
Rep: Reputation: 30
Thanks that works perfectly! I should have tried to approach it that way from the start instead of trying to ignore the errors!

Paddy
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP image handling/resizing benrose111488 Programming 1 07-07-2005 02:00 AM
Handling filenames with spaces in PHP gw1500se Programming 2 03-14-2004 01:03 PM
Perl Error Handling Crashed_Again Programming 1 01-21-2004 02:25 PM
handling downloads with php niehls Programming 0 02-09-2003 05:11 AM
PHP Link Handling Citizen Bleys Programming 1 10-10-2001 03:02 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration