我正在检查是否要输入if语句,并调试函数的结果,结果是它以字符串形式返回null。
<?php if($_product->getData('attr1') || $_product->getData('attr2')): ?> Do stuf here <?php endif; ?>
“attr2”返回“null”,一个长度等于4的字符串。
如何检查变量是否为null?
'null' 是一个字符串值,这就是它通过检查的原因。你需要像检查字符串值一样检查它。
'null'
<?php if($_product->getData('attr1') || ($_product->getData('attr2') && strtolower($_product->getData('attr2')) != 'null' ) ): ?> Do stuf here <?php endif; ?>
这是具体情况,如果你有 , 'Null' 或 NULL attr2
'Null'
NULL
attr2