3v4l.org

run code in 500+ PHP versions simultaneously
<?php $html = <<<'HTML' <p> <span> <strong> <span> Test </span> </strong> </span> </p> HTML; $doc = new \DOMDocument('1.0', 'UTF-8'); $doc->loadHTML( '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /></head><body>' . $html . '</body></html>' ); $xpath = new \DOMXPath($doc); $spans = []; foreach ($xpath->query('//span') as $span) { $spans[] = $span; } \assert(\count($spans) === 2); foreach ($spans as $span) { \assert($span instanceof \DOMElement); \assert($span->ownerDocument === $doc); } foreach ($spans as $span) { \assert($span instanceof \DOMElement); // This call will fail for the second `<span>`. It does not really // matter what is accessed here, the error message will be the same. $span->hasAttribute('test'); while ($span->childNodes->length) { $span->removeChild($span->childNodes[0]); } $span->appendChild( $span->ownerDocument->createTextNode('Hello World') ); } echo $span[0]->textContent . "\n"; // Hello World
Output for 8.3.0 - 8.3.35, 8.4.1 - 8.4.26, 8.5.0 - 8.5.11
Fatal error: Uncaught Error: Cannot use object of type DOMElement as array in /in/uSEC9:47 Stack trace: #0 {main} thrown in /in/uSEC9 on line 47
Process exited with code 255.
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.34, 8.2.0 - 8.2.34
Fatal error: Uncaught Error: Couldn't fetch DOMElement in /in/uSEC9:37 Stack trace: #0 /in/uSEC9(37): DOMElement->hasAttribute('test') #1 {main} thrown in /in/uSEC9 on line 37
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Warning: DOMElement::hasAttribute(): Couldn't fetch DOMElement in /in/uSEC9 on line 37 Warning: Couldn't fetch DOMElement. Node no longer exists in /in/uSEC9 on line 39 Notice: Undefined property: DOMElement::$childNodes in /in/uSEC9 on line 39 Notice: Trying to get property 'length' of non-object in /in/uSEC9 on line 39 Warning: Couldn't fetch DOMElement. Node no longer exists in /in/uSEC9 on line 43 Notice: Undefined property: DOMElement::$ownerDocument in /in/uSEC9 on line 43 Fatal error: Uncaught Error: Call to a member function createTextNode() on null in /in/uSEC9:43 Stack trace: #0 {main} thrown in /in/uSEC9 on line 43
Process exited with code 255.

preferences:
33.77 ms | 1144 KiB | 11 Q