0

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?

1 Answer 1

1

Use isset():

if(isset($xml->ContactAddress->Address5)) { fwrite($txt, $xml->ContactAddress->Address5 . "\r\n"); } 
Sign up to request clarification or add additional context in comments.

6 Comments

That gives me an error: PHP Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
Is that because you havent got the address5 tag in the first part though. For me I will always have <address5></address5> Just sometimes empty, sometimes not
Sorry if empty it is actually: <Address5/>
Sorry for that, misread the address line 5 is empty. Will update my answer momentarily.
Comparing with empty string works as well - see https://3v4l.org/4X94d
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.