a1anm
a1anm

Reputation: 1629

PHP Always not null

I have this piece of code in my cms:

<ul class="subsection_tabs" id="tab_group_one" style="clear:none;">
    <?php if ($this->getChildHtml('description')==NULL) { echo '<div id="trollweb_1"></div>'; } else echo('
    <li class="tab"><a href="javascript:void(0);" id="trollweb_1" onClick="trollweb_tabs(1)" class="active"><h4>' . $this->__('Product Description'). '</h4></a></li> '); ?>

The problem is it always outputs the 'else'. Even though I haven't filled in the description in the back end and it is blank.

How can I fix this?

Upvotes: 0

Views: 164

Answers (2)

VolkerK
VolkerK

Reputation: 96159

Maybe getChildHtml() always returns a string (not NULL). And maybe the string in your test environment only contains whitespaces.
In that case trim() removes them and you can simply check the length of the string

if ( 0<strlen(trim($this->getChildHtml('description'))) ) { 

Upvotes: 1

Residuum
Residuum

Reputation: 12064

If you try to not test only for NULL, but also for empty strings, then you should do

$childHtml = $this->getChildHtml('description');
if (empty($childHtml))

instead.

EDIT: As VolkerK said, empty('0') returns false as well, so that solution depends on your requirements. As it looks, like you are looking for string solutions, this is viable option, as long as you do not have '0' as values.

Upvotes: 1

Related Questions