SimpleXML a little note _php tutorial

Source: Internet
Author: User
SimpleXML provides a simple and fast method of XML operation, which greatly improves the efficiency of XML operation. But sometimes careless can bring a little trouble, look at the following code:

$xml = simplexml_load_string ( Title );

$title = $xml->title;

echo $title;

$xml->title = test;

echo $title;


Guess what the second output would be? It's test, not the imaginary title. Why is that? Here's why:

Echo GetType ($xml->title)//Object

Echo Get_class ($xml->title); SimpleXMLElement


See, $xml->title is an instance of a SimpleXMLElement class, not a string. So $title actually holds a reference to the SimpleXMLElement class, not a copy of the string. To get a copy of a string, you can only type conversions:

$title = (string) $xml->title; Get string

$xml->title = test;

echo $title; Output title


SimpleXMLElement should be implemented in a similar way to __tostring () interface (interested can go to see the source code of PHP, in "ext/simplexml/"), in the Echo and other expressions like a string. So there's another place to be aware of:

$_session[test] = $xml->title; Save a SimpleXMLElement variable to the session.

$_session[test] = (string) $xml->title; String

$_session[test] = Strval ($xml->title); That's OK.

http://www.bkjia.com/PHPjc/486307.html www.bkjia.com true http://www.bkjia.com/PHPjc/486307.html techarticle SimpleXML provides a simple and fast method of XML operation, which greatly improves the efficiency of XML operation. But sometimes careless will bring a little trouble, look at 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.