April 13, 2012

How to check whether domdocument has items

Question by BetterMan21

How to check whether domdocument has items

code:

 $iframe =  $dom->getElementsByTagName('iframe');


 foreach ($iframe as $if) {

    //how to check whether $if has items for
     // $if->item(0) so that I can access it without errors?
 }

Answer by Michael Berkowski

Test if the node has child nodes with hasChildNodes()

 foreach ($iframe as $if) {
    if ($if->hasChildNodes()) {
       // first child is $if->childNodes->item(0)
    }
 }

Answer by Starx

You can check using like

if($if -> hasChildNodes()) { ... }

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!