PHP: Whats's the difference between the SimpleXMLElement() class and SimpleXML_load_string()?

前端 未结 3 1801
伪装坚强ぢ
伪装坚强ぢ 2021-02-02 11:34

I\'ve seen alot of coders implement SimpleXML_load_string() instead of the SimpleXMLElement() class. Are there any benefits to using the former over th

3条回答
  •  庸人自扰
    2021-02-02 12:02

    It's mostly a convenience wrapper. With constructing the base element yourself, you need at least two lines of code to accomplish anything. With simplexml_load_string() a single expression might suffice:

     print simplexml_load_string($xml)->title;
    

    Is shorter than:

     $e = new SimpleXMLElement($xml);
     print $e->title;
    

    And then of course, there is also the slight variation in the function signature.

    Update: And exactly the same length as of

    print(new SimpleXMLElement($xml))->title;
    

提交回复
热议问题