skip to Main Content

According to the documentation, the DOMDocument::loadHTML() method "returns true on success or false on failure". My first question is how can it fail?

As well as correct HTML and very incorrect HTML, I have also tried feeding it complete, random, elbows-on-the-keyboard garbage with a few emojis added for good measure and it accepts it (adding the necessary tags to make an HTML document of it).

Likewise, the saveHTML method "returns the HTML, or false if an error occurred". So my second question is, under what circumstances can an error occur saving the HTML? The document exists because that’s what the function is being called on, so how can outputting the HTML fail?

When I use the loadHTML and saveHTML methods, I assume I should check they have not returned false, but I don’t understand when or why this could happen.

2

Answers


  1. The documentation doesn’t always specify what are all possible failure conditions. It should but sometimes there are too many and sometimes the conditions are too exotic. Ideally, PHP functions should not return false on error. They should throw an error instead.

    If you look at the PHP source code, you can see some examples of when loadHTML() would return false:

    • When $options is not an integer within PHP integer range.
    • When $source parameter is longer than PHP_INT_MAX.
    • When libxml returns a failure status. (You’d have to check with that library when it can fail.)
    Login or Signup to reply.
  2. The documentation is general, and for php 7.x, 5.x the following code will truly return false:

    var_dump(@(new DOMDocument())->loadHTML(""));
    

    UPDATE:
    Example for php 8.x and 7.x (thanks to @Dharman’s post)

    var_dump(@(new DOMDocument())->loadHTML("Yo!", 1111111111111111));
    

    But the interesting thing is that for PHP 5.6 this code will return true

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search