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 * @link */ /** * 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(), "\n.........................................\n\n"; $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.130.0000.01018.89
8.3.120.0030.00619.41
8.3.110.0100.00018.68
8.3.100.0030.00624.06
8.3.90.0080.00026.77
8.3.80.0070.00318.80
8.3.70.0170.00717.00
8.3.60.0040.01218.80
8.3.50.0100.00722.11
8.3.40.0040.01119.21
8.3.30.0090.00919.27
8.3.20.0040.00419.18
8.3.10.0040.00421.46
8.3.00.0070.00321.14
8.2.250.0090.00616.97
8.2.240.0030.00617.49
8.2.230.0090.00020.94
8.2.220.0110.00637.54
8.2.210.0040.00426.77
8.2.200.0060.00317.25
8.2.190.0120.00317.25
8.2.180.0120.00325.92
8.2.170.0150.00419.41
8.2.160.0070.00722.96
8.2.150.0030.00625.66
8.2.140.0040.00424.66
8.2.130.0050.00326.16
8.2.120.0080.00318.43
8.2.110.0100.00021.54
8.2.100.0040.00818.52
8.2.90.0030.00519.79
8.2.80.0030.00518.13
8.2.70.0030.00518.55
8.2.60.0000.00818.13
8.2.50.0100.00019.85
8.2.40.0030.00619.85
8.2.30.0040.00420.00
8.2.20.0040.00418.14
8.2.10.0040.00418.15
8.2.00.0050.00318.11
8.1.300.0080.00418.29
8.1.290.0090.00030.84
8.1.280.0120.00425.92
8.1.270.0040.00424.66
8.1.260.0090.00026.35
8.1.250.0030.00628.09
8.1.240.0060.00320.15
8.1.230.0060.00618.20
8.1.220.0030.00518.29
8.1.210.0040.00418.77
8.1.200.0060.00317.73
8.1.190.0000.00817.85
8.1.180.0030.00518.10
8.1.170.0080.00017.84
8.1.160.0040.00419.46
8.1.150.0050.00319.09
8.1.140.0030.00617.88
8.1.130.0040.00417.85
8.1.120.0050.00317.76
8.1.110.0000.00917.71
8.1.100.0040.00417.94
8.1.90.0030.00517.96
8.1.80.0000.00717.99
8.1.70.0040.00417.96
8.1.60.0000.00918.11
8.1.50.0060.00317.85
8.1.40.0030.00518.05
8.1.30.0090.00018.16
8.1.20.0080.00018.19
8.1.10.0050.00317.88
8.1.00.0000.00817.79
8.0.300.0070.00318.77
8.0.290.0000.00817.00
8.0.280.0040.00418.90
8.0.270.0050.00217.29
8.0.260.0040.00417.39
8.0.250.0040.00417.55
8.0.240.0000.00817.28
8.0.230.0000.00717.27
8.0.220.0070.00017.47
8.0.210.0050.00217.44
8.0.200.0030.00317.34
8.0.190.0050.00317.29
8.0.180.0000.00717.35
8.0.170.0000.00817.36
8.0.160.0040.00417.32
8.0.150.0030.00617.46
8.0.140.0060.00317.30
8.0.130.0060.00013.92
8.0.120.0040.00417.31
8.0.110.0000.00817.26
8.0.100.0040.00417.37
8.0.90.0050.00317.45
8.0.80.0090.01217.36
8.0.70.0040.00417.33
8.0.60.0040.00417.44
8.0.50.0040.00417.39
8.0.30.0130.00817.54
8.0.20.0090.01517.73
8.0.10.0090.00017.43
8.0.00.0120.01117.39
7.4.330.0000.00517.12
7.4.320.0030.00316.87
7.4.300.0000.00817.02
7.4.290.0040.00416.96
7.4.280.0000.00817.04
7.4.270.0070.00016.87
7.4.260.0030.00313.71
7.4.250.0040.00416.74
7.4.240.0020.00517.11
7.4.230.0000.00717.14
7.4.220.0130.01016.99
7.4.210.0060.01016.98
7.4.200.0000.00716.80
7.4.190.0040.00417.20
7.4.160.0100.00616.90
7.4.150.0070.01017.40
7.4.140.0110.00817.86
7.4.130.0030.01416.98
7.4.120.0100.01016.88
7.4.110.0000.01916.88
7.4.100.0090.01217.13
7.4.90.0100.01417.04
7.4.80.0060.01219.39
7.4.70.0100.00716.86
7.4.60.0070.01016.82
7.4.50.0080.00016.95
7.4.40.0030.01222.77
7.4.30.0100.01616.94
7.4.00.0080.00815.25
7.3.330.0000.00713.82
7.3.320.0060.00013.82
7.3.310.0030.00516.80
7.3.300.0070.00016.68
7.3.290.0090.00616.78
7.3.280.0100.00916.80
7.3.270.0030.01517.40
7.3.260.0130.00918.24
7.3.250.0120.01016.81
7.3.240.0070.01016.95
7.3.230.0130.00316.70
7.3.210.0100.00716.90
7.3.200.0090.00919.39
7.3.190.0120.00616.92
7.3.180.0130.00317.04
7.3.170.0050.01116.90
7.3.160.0100.00617.10
7.3.120.0080.00814.97
7.2.330.0140.01117.29
7.2.320.0100.00916.92
7.2.310.0100.01017.05
7.2.300.0100.00616.87
7.2.290.0080.00816.90
7.2.60.0200.00616.98
7.2.00.0030.01219.77
7.1.200.0000.01616.11
7.1.100.0030.00718.33
7.1.70.0040.00717.42
7.1.60.0080.00317.25
7.1.50.0100.01316.93
7.1.00.0070.04322.61
7.0.200.0040.00817.16
7.0.140.0000.07722.15
7.0.60.0030.05720.14
7.0.50.0130.07718.04
7.0.40.0200.07320.21
7.0.30.0300.03720.39
7.0.20.0200.04720.40
7.0.10.0000.05320.46
7.0.00.0100.04320.38
5.6.280.0070.07021.41
5.6.210.0200.06320.84
5.6.200.0130.05718.46
5.6.190.0030.04320.57
5.6.180.0000.08720.67
5.6.170.0370.06720.69
5.6.160.0000.05020.87
5.6.150.0100.07718.50
5.6.140.0070.08318.28
5.6.130.0070.05318.43
5.6.120.0030.04321.20
5.6.110.0100.08321.14
5.6.100.0000.04321.29
5.6.90.0100.05721.29
5.6.80.0130.04020.62
5.5.350.0070.07720.64
5.5.340.0030.08018.21
5.5.330.0070.06320.50
5.5.320.0030.07320.68
5.5.310.0270.06320.47
5.5.300.0070.05018.18
5.5.290.0030.06318.08
5.5.280.0000.04320.94
5.5.270.0130.03720.96
5.5.260.0170.06720.97
5.5.250.0100.07021.01
5.5.240.0000.04320.51
5.4.450.0200.04019.76
5.4.440.0330.07319.64
5.4.430.0230.04019.66
5.4.420.0230.04319.68
5.4.410.0230.04019.40
5.4.400.0200.04019.21
5.4.390.0170.04019.31
5.4.380.0170.04019.21
5.4.370.0130.04019.43
5.4.360.0200.03719.21
5.4.350.0170.03719.22
5.4.340.0170.05019.36
5.4.320.0300.03019.34
5.4.310.0130.04319.17
5.4.300.0230.07719.30
5.4.290.0170.04319.43
5.4.280.0270.06719.36
5.4.270.0200.04719.23
5.4.260.0230.03719.45
5.4.250.0230.03719.10
5.4.240.0330.03719.32
5.4.230.0300.07019.21
5.4.220.0330.06719.32
5.4.210.0330.04019.27
5.4.200.0230.04019.11
5.4.190.0330.04719.35
5.4.180.0300.04019.20
5.4.170.0300.04019.36
5.4.160.0270.04019.25
5.4.150.0300.08019.09
5.4.140.0430.04316.60
5.4.130.0370.06716.68
5.4.120.0230.03716.65
5.4.110.0430.05716.79
5.4.100.0270.06716.66
5.4.90.0430.05316.76
5.4.80.0370.03716.83
5.4.70.0330.06316.83
5.4.60.0230.04316.82
5.4.50.0470.05716.76
5.4.40.0400.06316.68
5.4.30.0300.04016.70
5.4.20.0330.05316.74
5.4.10.0400.05016.73
5.4.00.0230.05716.12
5.3.290.0200.04314.91
5.3.280.0400.04314.78
5.3.270.0330.03314.86
5.3.260.0270.03714.89
5.3.250.0230.04714.91
5.3.240.0270.05314.86
5.3.230.0270.06714.93
5.3.220.0300.03314.88
5.3.210.0230.05014.88
5.3.200.0330.07314.78
5.3.190.0230.04314.76
5.3.180.0400.06014.77
5.3.170.0400.07014.89
5.3.160.0230.04014.87
5.3.150.0370.07014.73
5.3.140.0300.07314.86
5.3.130.0270.03714.76
5.3.120.0330.04014.86
5.3.110.0400.06314.75
5.3.100.0300.06714.25
5.3.90.0270.05014.28
5.3.80.0230.04014.18
5.3.70.0230.04014.26
5.3.60.0230.06714.25
5.3.50.0270.04014.25
5.3.40.0230.05714.11
5.3.30.0200.04014.26
5.3.20.0270.03714.02
5.3.10.0200.04013.78
5.3.00.0300.04313.92
5.2.170.0270.03711.31
5.2.160.0330.04311.38
5.2.150.0230.03311.48
5.2.140.0170.03311.31
5.2.130.0200.03011.35
5.2.120.0200.04311.39
5.2.110.0230.04711.32
5.2.100.0270.05711.33
5.2.90.0300.02711.43
5.2.80.0270.04311.25
5.2.70.0300.04011.35
5.2.60.0270.05311.27
5.2.50.0330.04711.27
5.2.40.0170.04311.12
5.2.30.0170.05711.11
5.2.20.0200.04311.20
5.2.10.0270.05011.02
5.2.00.0200.05010.73
5.1.60.0230.03010.14
5.1.50.0200.02310.13
5.1.40.0200.04310.13
5.1.30.0230.03010.48
5.1.20.0170.03710.50
5.1.10.0300.04010.23
5.1.00.0270.04710.21
5.0.50.0100.0308.71
5.0.40.0100.0208.67
5.0.30.0170.0478.49
5.0.20.0200.0138.37
5.0.10.0100.0178.35
5.0.00.0130.0308.32
4.4.90.0100.0337.81
4.4.80.0100.0337.81
4.4.70.0130.0307.81
4.4.60.0100.0307.81
4.4.50.0100.0177.81
4.4.40.0130.0237.81
4.4.30.0200.0137.81
4.4.20.0070.0377.81
4.4.10.0100.0277.81
4.4.00.0130.0407.81
4.3.110.0100.0137.81
4.3.100.0100.0177.81
4.3.90.0130.0107.81
4.3.80.0130.0207.81
4.3.70.0100.0137.81
4.3.60.0070.0177.81
4.3.50.0100.0337.81
4.3.40.0100.0307.81
4.3.30.0030.0277.81
4.3.20.0100.0337.81
4.3.10.0070.0137.81
4.3.00.0070.0207.81

preferences:
28.58 ms | 403 KiB | 5 Q