How can SimpleXML attributes be accessed and displayed in PHP?

To access and display SimpleXML attributes in PHP, you can use the attributes() method to retrieve the attributes of an XML element. You can then loop through the attributes and display their values as needed.

$xml = simplexml_load_string('<book genre="fiction"><title>Harry Potter</title></book>');

foreach ($xml->attributes() as $key => $value) {
    echo $key . ': ' . $value . '<br>';
}