Edward144
Edward144

Reputation: 493

PHP simplexml_load_file - ignore xml object if it's empty

I am using simplexml_load_file to gather information from an xml and put it in relevant places in a txt file.

I want to ignore certain parts if they are empty. For example if I have address line 1 to 5, and address line 5 is empty then I want it to be ignored.

Here is what I have tried to ignore empty objects:

if($xml->ContactAddress->Address5 != '') {
    fwrite($txt, $xml->ContactAddress->Address5 . "\r\n");
}

if($xml->ContactAddress->Address5) {
    fwrite($txt, $xml->ContactAddress->Address5 . "\r\n");
}

With both of these a blank line is still being inputted, so what is the correct way to check if t is blank or not?

Upvotes: 0

Views: 136

Answers (1)

Jirka Hrazdil
Jirka Hrazdil

Reputation: 4021

Use isset():

if(isset($xml->ContactAddress->Address5)) {
    fwrite($txt, $xml->ContactAddress->Address5 . "\r\n");
}

Upvotes: 1

Related Questions