0

I'm checking whether to enter an if statement and debugging the result of a function it turn out that it returns null as a string.

In my code I'm checking:

<?php if($_product->getData('attr1') || $_product->getData('attr2')): ?>
      Do stuf here
<?php endif; ?>

And 'attr2' returns 'null', a string with length equals to 4.

How can I check if the variable is null for real?

4
  • Is your problem that getData() returns 'null' instead of null? Handle it there. Commented Sep 8, 2018 at 9:11
  • @simPod yes the problem is that, isn't there a way to check here in my template that the variable is null instead of 'null'? Commented Sep 8, 2018 at 9:12
  • 1
    You'd probably have to add a condition to check for 'null'. IMO the proper way to handle this is to fix getData method to return null. It will ease your pain in all places where you use it then. Because what if attr2 has really a 'null' value? Commented Sep 8, 2018 at 9:14
  • @simPod is saying absolute right, you need to add another string comparison for 'null' value. Commented Sep 8, 2018 at 9:19

1 Answer 1

1

'null' is a string value that's why it is passing your checks. you need to check it like string value as well.

<?php if($_product->getData('attr1') || ($_product->getData('attr2') && strtolower($_product->getData('attr2')) != 'null' ) ): ?>
      Do stuf here
<?php endif; ?>

It is case specific, if you have 'null', 'Null' or NULL string for only attr2

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.