显示字段的PHP条件语句

显示字段的PHP条件语句,php,magento,Php,Magento,我已经插入了一个代码来在view.phptml中显示产品属性,这很好。但它显示一个空框,即使字段为空 我用来显示的代码是这样的 <div class="add-to-box"> <div class="add-to-cart"><div>image path</div> <h3><?php echo $_helper->productAttribute($_product, $_product->get

我已经插入了一个代码来在view.phptml中显示产品属性,这很好。但它显示一个空框,即使字段为空

我用来显示的代码是这样的

<div class="add-to-box">        
<div class="add-to-cart"><div>image path</div>
<h3><?php
echo $_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift')
?></h3></div></div>

图像路径

如何在上述语句中添加一个条件,以便在字段为空时隐藏空框,使其不显示

使用
为空
,如下所示:

<?php
    if( !empty( $_product->getFreeGift() ) ){
?>

... HTML here ...

<?php
    }
?>

... 这里是HTML。。。


... 这里是HTML。。。

你好,你可以用这个

<?php 
if (isset($_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift'))) {
echo('<div class="add-to-box">        
         <div class="add-to-cart">
         <div>image path</div>
          <h3>'.$_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift').'</h3>
          </div>
      </div>');
} else {
   ..... // other stuff
}

Simple
if-else
语句不起作用?
<?php 
if (isset($_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift'))) {
echo('<div class="add-to-box">        
         <div class="add-to-cart">
         <div>image path</div>
          <h3>'.$_helper->productAttribute($_product, $_product->getFreeGift(), 'free_gift').'</h3>
          </div>
      </div>');
} else {
   ..... // other stuff
}