13

I have a XML setting I am trying to load using simplexml. The XML setting can be edited by the user of a web application. I want to handle errors myself and dispatch a warning message to the user interface. However, simplexml keeps throwing warnings on malformed XML instead of quietly returning false.

How can I make simpleXML shut up and not throw a warning? Is there an option for that?

Pekka
  • 442,112
  • 142
  • 972
  • 1,088

2 Answers2

18

Use libxml_use_internal_errors() to suppress XML errors.

You can then use libxml_get_errors() to inspect any errors if you need to.

Ben James
  • 121,135
  • 26
  • 193
  • 155
3

Put an @ in front of the function that is throwing the warning, this will suppress all warning messages.

RMcLeod
  • 2,561
  • 1
  • 22
  • 38
  • 2
    You should never, ever use error suppression. I have not found a case yet in PHP where by proper testing (e.g. as per the accepted answer) you can test for failure before executing the function. Error suppression will often come back and bite you, because you can never trace back the original error (because you suppressed it). It is best practice to handle the failure case, rather than hiding the issue. A common example is to use `file_exists`/`is_readable` before you open files. Lastly, error suppression has a slight performance hit (but this is just a *small* bonus). – catchdave Jun 01 '13 at 02:04
  • 1
    Whats the purpose of having error suppression (@) then??? – Obaid Maroof Aug 12 '13 at 09:42