SimpleXMLElement::__toString

SimpleXMLElement is used to represent an XML element.  The typical usage is:

$resp = simplexml_load_file($httpcall);

$resp->somechild;

 

Note that somechild is the tag name of a child of the XML element that $resp represents.

If we  want to extract the text within the tag, using $resp->somechild is usually enough because the SimpleXMLElement object $resp->somechild can be casted into a string when appropriate. But sometimes we need to use SimpleXMLElement ::__toString() to convert the SimpleXMLElement object to string explicitly, i.e., extract the text within the tag. For example, we can use if($resp->somechild==”12345″) to compare the string between the XML tags with “12345”, that is ok because $resp->somechild can be converted to string automatically. But we cannot use if($resp->somechild==$resp->anotherchild) to compare the text of two children of $resp. Even the text of the two children are the same, the result of the comparison is not equal since they are two different objects. In this case, we have to use SimpleXMLElement ::__toString() to convert them to strings before comparison, i.e. if($resp->somechild->__toString()==$resp->anotherchild->__toString())

Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Comments are closed, but trackbacks and pingbacks are open.