A. Litsa
A. Litsa

Reputation: 31

PHP: How can I write IF statement that detects when XML value is NULL?

I'm having trouble figuring out how to get my if statement to recognize when an XML value is NULL. It seems as though no matter what I try, the <li>'s are still printed.

// PHP sample
$a = $xml->sentence1;
$b = $xml->sentence2;

if ( isset($a) || !empty($b) ){
    echo '<ul>';
    if ($a !== NULL){
        echo '<li>' . $a . '</li>';
    }
    if ($b !== ''){
        echo '<li>' . $b . '</li>';
    }
    echo '</ul>';
}

// XML sample
<sentence1></sentence1>

As you can see, I tried all sorts of stuff — NULL, isset(), empty() — but the <li>'s always render.

Upvotes: 1

Views: 40

Answers (1)

A. Litsa
A. Litsa

Reputation: 31

This worked

if ( $a == true || $b == true ){
    echo '<ul>';
    if ($a == true){
        echo '<li>' . $a . '</li>';
    }
    if ($b == true){
        echo '<li>' . $b . '</li>';
    }
    echo '</ul>';
}

This makes me think that boolean functions (like "isset()") were overkill in this case, but I wonder why they did not work nonetheless.

Upvotes: 1

Related Questions