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
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:$options
is not an integer within PHP integer range.$source
parameter is longer than PHP_INT_MAX.The documentation is general, and for php 7.x, 5.x the following code will truly return
false
:UPDATE:
Example for php 8.x and 7.x (thanks to @Dharman’s post)
But the interesting thing is that for PHP 5.6 this code will return
true