3v4l.org

run code in 300+ PHP versions simultaneously
<?php $a = '<var>One</var> <var>Two</var>'; // Works fine ($domA = new DOMDocument('1.0', 'UTF-8'))->loadHTML($a); foreach ((new DOMXPath($domA))->query('//var') as $var) { $var->replaceWith($domA->createElement('p', $var->nodeValue)); } var_dump($domA->saveHTML()); $b = '<var>One</var><var>Two</var>'; // Causes a 'double free' error ($domB = new DOMDocument('1.0', 'UTF-8'))->loadHTML($b); foreach ((new DOMXPath($domB))->query('//var') as $var) { $var->replaceWith($domB->createElement('p', $var->nodeValue)); } var_dump($domB->saveHTML());
Output for 8.1.19 - 8.1.33, 8.2.6 - 8.2.29, 8.3.0 - 8.3.25, 8.4.1 - 8.4.12
string(155) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><p>One</p> <p>Two</p></body></html> " string(154) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><p>One</p><p>Two</p></body></html> "
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.18, 8.2.0 - 8.2.5
string(155) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><p>One</p> <p>Two</p></body></html> " string(158) "<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <html><body><p>One</p><var>Two</var></body></html> " free(): double free detected in tcache 2
Process exited with code 134.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Call to undefined method DOMElement::replaceWith() in /in/Y2vJc:8 Stack trace: #0 {main} thrown in /in/Y2vJc on line 8
Process exited with code 255.

preferences:
101.17 ms | 409 KiB | 5 Q