3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $xml = <<< XML <?xml version="1.0" encoding="utf-8"?> <books> <book>Patterns of Enterprise Application Architecture</book> <book></book> <book>Design Patterns: Elements of Reusable Software Design</book> <book>Clean Code</book> </books> XML; $dom = new DOMDocument; $dom->loadXML($xml); $books = $dom->getElementsByTagName('book'); $length = $books->length; $break = FALSE; foreach ($books as $k => $book) { var_dump($book); if($break){ if($k == ($length)) { echo "last iteration: " . $book->nodeValue; } else { continue; } } if(empty($book)) { $break = TRUE; continue; } echo $book->nodeValue, PHP_EOL; }
based on vHboC
Output for 7.0.0 - 7.2.0
object(DOMElement)#4 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(47) "Patterns of Enterprise Application Architecture" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(47) "Patterns of Enterprise Application Architecture" } Patterns of Enterprise Application Architecture object(DOMElement)#5 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(0) "" } object(DOMElement)#4 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(53) "Design Patterns: Elements of Reusable Software Design" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(53) "Design Patterns: Elements of Reusable Software Design" } Design Patterns: Elements of Reusable Software Design object(DOMElement)#5 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(10) "Clean Code" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(10) "Clean Code" } Clean Code
Output for 5.3.19 - 5.3.29, 5.4.1 - 5.6.28
object(DOMElement)#3 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(47) "Patterns of Enterprise Application Architecture" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(47) "Patterns of Enterprise Application Architecture" } Patterns of Enterprise Application Architecture object(DOMElement)#5 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(0) "" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> NULL ["lastChild"]=> NULL ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(0) "" } object(DOMElement)#3 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(53) "Design Patterns: Elements of Reusable Software Design" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(53) "Design Patterns: Elements of Reusable Software Design" } Design Patterns: Elements of Reusable Software Design object(DOMElement)#5 (18) { ["tagName"]=> string(4) "book" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(4) "book" ["nodeValue"]=> string(10) "Clean Code" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(4) "book" ["baseURI"]=> string(1) "/" ["textContent"]=> string(10) "Clean Code" } Clean Code
Output for 5.4.0
object(DOMElement)#3 (0) { } Patterns of Enterprise Application Architecture object(DOMElement)#5 (0) { } object(DOMElement)#3 (0) { } Design Patterns: Elements of Reusable Software Design object(DOMElement)#5 (0) { } Clean Code