3v4l.org

run code in 300+ PHP versions simultaneously
<?php $doc = new DOMDocument(); $doc->loadHTML('<html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a></body></html>'); $links = $doc->getElementsByTagName('a'); print_r($links); foreach($links as $link) { echo $link->getAttribute('href') . "\n"; } echo $doc->saveHTML() . "\n\n"; $body = $doc->getElementsByTagName('body')[0]; $select = $doc->createElement('select'); $body->appendChild($select); echo $doc->saveHTML() . "\n\n"; function addElement($doc, $newElement, $parentElement, $attributesArray = null) { $pe = $doc->getElementsByTagName($parentElement)[0]; $ne = $doc->createElement($newElement); $pe->appendChild($ne); if (isset($attributesArray)) { while ( ($current = current($attributesArray)) ) { print_r($current); $attrib = key($current); $domAttribute = $doc->createAttribute($attrib); // Value for the created attribute $domAttribute->value = $current[$attrib]; // Don't forget to append it to the element $ne->appendChild($domAttribute); echo $attrib . '=' . $current[$attrib] . "\n"; next($array); } } echo $doc->saveHTML(); } addElement($doc, 'ul', 'select');
Output for 5.6.3 - 5.6.24, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.5 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
DOMNodeList Object ( [length] => 2 ) teste.php ../teste2.php <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a></body></html> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a><select></select></body></html> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a><select><ul></ul></select></body></html>
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 DOMNodeList Object ( [length] => 2 ) teste.php ../teste2.php <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a></body></html> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a><select></select></body></html> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a><select><ul></ul></select></body></html>
Output for 5.6.0 - 5.6.2
DOMNodeList Object ( [length] => 2 ) teste.php ../teste2.php <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><a href="teste.php">Teste</a><a href="../teste2.php">Teste2</a></body></html> Fatal error: Cannot use object of type DOMNodeList as array in /in/QMK3v on line 17
Process exited with code 255.

preferences:
163.06 ms | 402 KiB | 213 Q