How can SimpleXML be used to access attributes and attribute names in PHP?

To access attributes and attribute names in SimpleXML in PHP, you can use the `attributes()` method to retrieve all attributes of an element as an array. You can then loop through this array to access the attribute names and their values.

$xml = '<bookstore>
            <book category="fiction">
                <title>Harry Potter</title>
                <author>J.K. Rowling</author>
            </book>
        </bookstore>';

$simplexml = simplexml_load_string($xml);

foreach ($simplexml->book->attributes() as $name => $value) {
    echo "Attribute name: $name, Value: $value <br>";
}