$xml = <<XML; $dom = new DOMDocument(); $dom->loadXML($xml); $book = $dom->getElementsByTagName('book')[0]; if ($book->hasAttribute('author')) { echo 'The "author" attribute is present in the "book" element'; }
The "author" attribute is present in the "book" elementThis function is part of the PHP DOM extension, which is included in the PHP core. No additional library is required.