Notes for SimpleXML _ PHP Tutorial

Source: Internet
Author: User
Some notes about SimpleXML. SimpleXML provides a simple and fast XML operation method, greatly improving the efficiency of XML operations. But sometimes it may cause a lot of trouble to be careful. look at the following code: $ xmls SimpleXML provides a simple and fast XML operation method, greatly improving the efficiency of XML operations. But sometimes it may cause a lot of trouble if you are not careful. check the following code:

$ Xml = simplexml_load_string ( Title );

$ Title = $ xml-> title;

Echo $ title;

$ Xml-> title = test;

Echo $ title;


Guess what the second output result will be? It is test, not the title in imagination. Why? The reason is as follows:

Echo gettype ($ xml-> title) // object

Echo get_class ($ xml-> title); // SimpleXMLElement


Can you see that $ xml-> title is an instance of the SimpleXMLElement class, not a string. Therefore, $ title actually stores a reference to the SimpleXMLElement class, rather than a copy of the string. To obtain a copy of a string, you can only perform type conversion:

$ Title = (string) $ xml-> title; // Obtain the string

$ Xml-> title = test;

Echo $ title; // output title


SimpleXMLElement should implement an interface similar to _ tostring () (if you are interested, check the PHP source code in "ext/simplexml ), can be expressed in expressions such as echo similar to a string. Note the following points:

$ _ SESSION [test] = $ xml-> title; // Save a SimpleXMLElement variable to the SESSION.

$ _ SESSION [test] = (string) $ xml-> title; // string

$ _ SESSION [test] = strval ($ xml-> title); // This is also true.

Bytes. But sometimes it may cause a lot of trouble if you are not careful about it. See the following code: $ xml = s...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.