3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * In SimpleXML, how can I add an existing SimpleXMLElement as a child element? * @link http://stackoverflow.com/q/767327/367456 */ /** * Insert XML into a SimpleXMLElement * * @param SimpleXMLElement $parent * @param string $xml * @param bool $before * @return bool XML string added */ function simplexml_import_xml(SimpleXMLElement $parent, $xml, $before = false) { $xml = (string)$xml; // check if there is something to add if ($nodata = !strlen($xml) or $parent[0] == NULL) { return $nodata; } // add the XML $node = dom_import_simplexml($parent); $fragment = $node->ownerDocument->createDocumentFragment(); $fragment->appendXML($xml); if ($before) { return (bool)$node->parentNode->insertBefore($fragment, $node); } return (bool)$node->appendChild($fragment); } $parent = new SimpleXMLElement('<parent/>'); // insert some XML simplexml_import_xml($parent, "\n <test><this>now</this></test>\n"); // insert some XML before a certain element, here the first <test> element // that was just added simplexml_import_xml($parent->test, "<!-- leave a comment -->\n ", $before = true); // you can place comments above the root element simplexml_import_xml($parent, "<!-- this works, too -->", $before = true); // but take care, you can produce invalid XML, too: // simplexml_add_xml($parent, "<warn><but>take care!</but> you can produce invalid XML, too</warn>", $before = true); // echo $parent->asXML(); $test = simplexml_load_string($parent->asXML()); // validate /** * Insert SimpleXMLElement into SimpleXMLElement * * @param SimpleXMLElement $parent * @param SimpleXMLElement $child * @param bool $before * @return bool SimpleXMLElement added */ function simplexml_import_simplexml(SimpleXMLElement $parent, SimpleXMLElement $child, $before = false) { // check if there is something to add if ($child[0] == NULL) { return true; } // if it is a list of SimpleXMLElements default to the first one $child = $child[0]; // insert attribute if ($child->xpath('.') != array($child)) { $parent[$child->getName()] = (string)$child; return true; } $xml = $child->asXML(); // remove the XML declaration on document elements if ($child->xpath('/*') == array($child)) { $pos = strpos($xml, "\n"); $xml = substr($xml, $pos + 1); } return simplexml_import_xml($parent, $xml, $before); } // append the element itself to itself simplexml_import_simplexml($parent, $parent); // insert <this> before the first child element (<test>) simplexml_import_simplexml($parent->children(), $parent->test->this, true); // add an attribute to the document element $test = new SimpleXMLElement('<test attribute="value" />'); simplexml_import_simplexml($parent, $test->attributes()); echo $parent->asXML();

Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).

VersionSystem time (s)User time (s)Memory (MiB)
8.3.60.0120.00618.81
8.3.50.0070.01317.96
8.3.40.0100.00619.28
8.3.30.0090.00619.04
8.3.20.0080.00019.14
8.3.10.0070.01319.18
8.3.00.0060.00319.76
8.2.180.0190.00017.00
8.2.170.0100.00622.96
8.2.160.0060.00919.32
8.2.150.0060.00324.18
8.2.140.0060.00324.66
8.2.130.0050.00326.16
8.2.120.0100.00019.31
8.2.110.0030.00722.67
8.2.100.0060.00718.28
8.2.90.0080.00018.41
8.2.80.0000.01118.30
8.2.70.0040.00417.87
8.2.60.0000.00818.55
8.2.50.0070.00318.10
8.2.40.0050.00318.66
8.2.30.0060.00318.11
8.2.20.0000.01118.08
8.2.10.0000.00818.20
8.2.00.0040.00418.16
8.1.280.0130.00625.92
8.1.270.0080.00023.99
8.1.260.0080.00026.35
8.1.250.0000.00828.09
8.1.240.0030.00622.85
8.1.230.0060.00621.50
8.1.220.0050.00318.29
8.1.210.0060.00319.47
8.1.200.0070.00317.98
8.1.190.0000.00917.73
8.1.180.0000.00918.10
8.1.170.0040.00418.99
8.1.160.0070.00019.38
8.1.150.0030.00519.05
8.1.140.0030.00517.92
8.1.130.0040.00417.92
8.1.120.0070.00017.98
8.1.110.0040.00417.97
8.1.100.0080.00018.00
8.1.90.0040.00417.76
8.1.80.0040.00417.82
8.1.70.0000.00717.85
8.1.60.0000.00817.93
8.1.50.0080.00017.93
8.1.40.0040.00417.92
8.1.30.0030.00618.13
8.1.20.0030.00518.13
8.1.10.0040.00418.00
8.1.00.0060.00618.05
8.0.300.0060.00320.48
8.0.290.0050.00217.25
8.0.280.0060.00318.84
8.0.270.0080.00017.57
8.0.260.0030.00317.28
8.0.250.0040.00417.22
8.0.240.0070.00017.46
8.0.230.0000.00717.50
8.0.220.0000.00717.47
8.0.210.0080.00017.37
8.0.200.0060.00017.40
8.0.190.0080.00017.37
8.0.180.0030.00517.22
8.0.170.0030.00617.39
8.0.160.0000.00717.25
8.0.150.0060.00317.39
8.0.140.0040.00717.38
8.0.130.0000.00613.88
8.0.120.0080.00017.43
8.0.110.0040.00417.18
8.0.100.0080.00017.35
8.0.90.0080.00017.29
8.0.80.0110.00717.25
8.0.70.0020.00517.22
8.0.60.0040.00417.32
8.0.50.0040.00417.36
8.0.30.0090.00917.45
8.0.20.0070.01517.56
8.0.10.0040.00417.30
8.0.00.0080.01217.20
7.4.330.0020.00213.51
7.4.320.0000.00717.01
7.4.300.0070.00016.85
7.4.290.0000.00716.88
7.4.280.0070.00316.90
7.4.270.0040.00416.99
7.4.260.0040.00313.79
7.4.250.0000.00817.00
7.4.240.0000.00717.01
7.4.230.0070.00317.08
7.4.220.0160.00917.02
7.4.210.0080.00817.05
7.4.200.0050.00217.13
7.4.190.0000.00716.97
7.4.160.0160.00017.00
7.4.150.0110.00716.92
7.4.140.0080.01116.86
7.4.130.0130.00416.90
7.4.120.0100.01316.86
7.4.110.0140.00916.93
7.4.100.0030.01616.81
7.4.90.0050.01316.93
7.4.80.0040.01819.39
7.4.70.0130.01316.98
7.4.60.0100.00717.04
7.4.50.0030.00616.86
7.4.40.0120.00416.94
7.4.30.0100.00716.84
7.4.00.0150.00014.95
7.3.330.0030.00313.61
7.3.320.0030.00313.80
7.3.310.0000.00716.84
7.3.300.0000.00716.79
7.3.290.0070.00716.68
7.3.280.0100.01016.74
7.3.270.0030.01516.89
7.3.260.0180.00316.93
7.3.250.0090.00916.82
7.3.240.0130.00916.89
7.3.230.0120.00616.83
7.3.210.0120.00616.85
7.3.200.0110.00716.91
7.3.190.0110.00617.13
7.3.180.0030.01616.71
7.3.170.0130.00316.75
7.3.160.0120.00716.69
7.3.120.0000.01315.11
7.2.330.0030.01617.28
7.2.320.0000.01717.00
7.2.310.0130.01017.29
7.2.300.0100.00717.30
7.2.290.0120.00617.01
7.2.00.0030.01319.75
7.1.100.0000.01218.22
7.1.70.0040.01117.19
7.1.60.0040.01519.30
7.1.50.0070.01117.16
7.1.00.0030.07022.62
7.0.200.0000.00816.74
7.0.140.0070.07022.41
7.0.120.0030.03322.28
7.0.60.0030.09020.14
7.0.50.0070.08318.11
7.0.40.0030.04720.25
7.0.30.0300.06320.28
7.0.20.0270.07020.38
7.0.10.0030.09320.29
7.0.00.0100.08020.31
5.6.280.0100.06321.27
5.6.210.0000.04320.82
5.6.200.0000.04318.39
5.6.190.0030.04720.66
5.6.180.0330.06320.87
5.6.170.0300.07720.76
5.6.160.0130.07320.67
5.6.150.0070.03718.47
5.6.140.0000.06718.47
5.6.130.0100.05318.40
5.6.120.0070.04021.32
5.6.110.0100.07721.23
5.6.100.0100.06721.17
5.6.90.0030.07721.16
5.6.80.0130.07720.62
5.5.350.0000.03720.69
5.5.340.0100.08318.22
5.5.330.0030.04320.73
5.5.320.3900.03320.48
5.5.310.0270.06020.57
5.5.300.0000.06018.20
5.5.290.0070.03718.24
5.5.280.0070.08320.95
5.5.270.0100.04020.93
5.5.260.0070.07721.10
5.5.250.0030.06721.01
5.5.240.0030.08320.40
5.4.450.0400.05019.81
5.4.440.0670.03319.80
5.4.430.0600.04019.53
5.4.420.0530.04019.77
5.4.410.0500.04719.42
5.4.400.0570.03319.41
5.4.390.0270.04019.47
5.4.380.0530.04719.45
5.4.370.0570.03719.39
5.4.360.0270.04019.34
5.4.350.0770.03719.15
5.4.340.0570.03719.30
5.4.320.0630.03719.30
5.4.310.0670.04719.16
5.4.300.0370.04019.17
5.4.290.0500.03019.29
5.4.280.0830.04019.44
5.4.270.0300.04719.20
5.4.260.0900.03319.22
5.4.250.0630.04319.20
5.4.240.0830.03719.49
5.4.230.0870.04319.29
5.4.220.0400.04319.29
5.4.210.0600.04719.45
5.4.200.0870.04019.28
5.4.190.1000.03019.45
5.4.180.0600.05019.45
5.4.170.0600.03319.38
5.4.160.0370.04019.10
5.4.150.0600.03719.16
5.4.140.0600.03316.89
5.4.130.0670.03716.62
5.4.120.0600.04016.67
5.4.110.0670.03316.80
5.4.100.0400.03316.57
5.4.90.0630.03316.58
5.4.80.0400.03016.72
5.4.70.0630.03316.72
5.4.60.0630.03016.79
5.4.50.0500.04316.74
5.4.40.0570.03316.63
5.4.30.0470.03716.79
5.4.20.0470.03716.73
5.4.10.0800.03316.72
5.4.00.0670.04016.24
5.3.290.0570.04716.24
5.3.280.0800.04016.24
5.3.270.0570.04016.24
5.3.260.0770.03716.24
5.3.250.0770.04316.24
5.3.240.0600.03016.24
5.3.230.0570.03716.24
5.3.220.0600.04016.24
5.3.210.0430.04016.24
5.3.200.0430.04016.24
5.3.190.0370.04016.24
5.3.180.0730.04716.24
5.3.170.0530.03316.24
5.3.160.0730.03716.24
5.3.150.0470.03716.24
5.3.140.0530.04016.24
5.3.130.0600.04316.24
5.3.120.0500.04316.24
5.3.110.0770.03316.24
5.3.100.0670.03316.24
5.3.90.0430.04316.24
5.3.80.0700.04316.24
5.3.70.0670.04016.24
5.3.60.1030.04016.24
5.3.50.0530.03016.24
5.3.40.0930.03016.24
5.3.30.0670.03716.24
5.3.20.0630.04316.24
5.3.10.0430.03716.24
5.3.00.0530.04716.24
5.2.170.0700.02716.24
5.2.160.0200.02716.24
5.2.150.0500.02716.24
5.2.140.0170.03016.24
5.2.130.0800.02716.24
5.2.120.0330.04016.24
5.2.110.0770.03016.24
5.2.100.0300.03316.23
5.2.90.0170.03316.23
5.2.80.0500.03316.23
5.2.70.0470.03316.23
5.2.60.0430.03716.23
5.2.50.0330.04316.23
5.2.40.0400.02716.23
5.2.30.0370.03316.23
5.2.20.0370.02316.23
5.2.10.0230.03016.23
5.2.00.0230.03016.23
5.1.60.0470.02016.23
5.1.50.0370.02316.23
5.1.40.0500.02316.23
5.1.30.0470.02716.23
5.1.20.0300.03016.23
5.1.10.0600.02016.23
5.1.00.0670.02016.23
5.0.50.0170.01716.23
5.0.40.0270.02016.23
5.0.30.0200.03016.23
5.0.20.0100.02016.23
5.0.10.0230.02716.23
5.0.00.0170.02716.23
4.4.90.0230.01016.23
4.4.80.0170.01716.23
4.4.70.0070.01716.23
4.4.60.0170.01716.23
4.4.50.0300.01316.23
4.4.40.0230.02016.23
4.4.30.0130.01716.23
4.4.20.0170.01716.23
4.4.10.0300.01316.23
4.4.00.0100.02016.23
4.3.110.0170.01316.23
4.3.100.0230.02016.23
4.3.90.0270.01716.23
4.3.80.0300.02016.23
4.3.70.0200.01316.23
4.3.60.0070.01316.23
4.3.50.0200.02016.23
4.3.40.0100.02716.23
4.3.30.0070.01316.23
4.3.20.0130.01316.23
4.3.10.0170.01716.23
4.3.00.0070.02016.23

preferences:
49.7 ms | 404 KiB | 6 Q