How can PHP be used to parse and process data stored in different formats, such as JSON or XML?

To parse and process data stored in different formats such as JSON or XML in PHP, you can use built-in functions like json_decode() for JSON data and simplexml_load_string() for XML data. These functions allow you to easily convert the data into PHP arrays or objects, which can then be manipulated and processed as needed.

// Example of parsing JSON data
$json_data = '{"name": "John", "age": 30}';
$decoded_data = json_decode($json_data);

echo $decoded_data->name; // Output: John
echo $decoded_data->age; // Output: 30

// Example of parsing XML data
$xml_data = '<person><name>John</name><age>30</age></person>';
$decoded_data = simplexml_load_string($xml_data);

echo $decoded_data->name; // Output: John
echo $decoded_data->age; // Output: 30