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 ...