Jinbom Heo
Jinbom Heo

Reputation: 7400

which is better expression to make string into xml object[php]?

which is better expression to make string into xml object[php]?

$xml = new SimpleXMLElement($xml_string);

vs

$xml = simplexml_load_string($xml_string);

same?

Upvotes: 1

Views: 240

Answers (3)

shamittomar
shamittomar

Reputation: 46692

The simplexml_load_string lets you specify the class of which you want to get the object of. If you call the simplexml_load_string without specifying the class as second parameter, then it automatically gives you the object of SimpleXMLElement.

So, in the way you are running it, they both will give you same results.

Upvotes: 2

GolezTrol
GolezTrol

Reputation: 116110

They're essentially the same. Which one you use is based on personal taste. I'd go for the first. The simplexml_load_string function will create an object too, which makes it essentially an alias for the constructor.

Upvotes: 2

deceze
deceze

Reputation: 522135

They're basically identical. SimpleXMLElement::__construct accepts data strings or file paths, if you set the appropriate options. simplexml_load_file and simplexml_load_string are basically convenience function wrappers that amount to the same thing as new SimpleXMLElement() with the correct options set.

Upvotes: 2

Related Questions