How does PHP handle XML data when stored as a string variable?

PHP can handle XML data stored as a string variable by using functions like simplexml_load_string() to parse the XML string and convert it into an object that can be easily manipulated. This allows developers to access and modify the XML data using PHP code.

$xmlString = "<data><name>John Doe</name><age>30</age></data>";

$xmlObject = simplexml_load_string($xmlString);

// Accessing XML data as an object
echo "Name: " . $xmlObject->name . "\n";
echo "Age: " . $xmlObject->age . "\n";

// Modifying XML data
$xmlObject->age = 31;

// Converting back to XML string
$updatedXmlString = $xmlObject->asXML();

echo $updatedXmlString;