条件if语句不工作php版本

时间:2015-04-28 10:28:56

标签: php if-statement conditional

使用Boostrap和PHP如果字段C存在,我希望我的代码使用(class="col-sm-6")显示两个字段。如果不是,则仅使用(class="col-sm-12")显示字段B.它无法正常工作。请帮忙 ?

PHP 5.4 ...我没有收到任何错误,但是如果使用PHP 5.3时语句不能按照我想要的方式运行...我得到了一个(Notice: Undefined property: stdClass::$gem_gemstone_para_c in)错误

这是我的代码。

<?php if (is_null($row->gem_gemstone_para_c)){ ?>
 <div class="post row">
  <div class="col-sm-6">
   <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_b; ?>" class="img-responsive" alt="" />
  </div>
  <div class="col-sm-6">
   <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_c; ?>" class="img-responsive" alt="" />
  </div>
</div>
<?php } else { ?>
<div class="post row">
    <div class="col-sm-12">
        <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_b; ?>" class="img-responsive" alt="" />
        </div>
    </div>
 

1 个答案:

答案 0 :(得分:2)

您必须检查属性是否已设置且不为null。 PHP中有一个function正是如此。您的代码看起来像这样。

<?php if (!isset($row->gem_gemstone_para_c)){ ?>
 <div class="post row">
  <div class="col-sm-6">
   <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_b; ?>" class="img-responsive" alt="" />
  </div>
  <div class="col-sm-6">
   <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_c; ?>" class="img-responsive" alt="" />
  </div>
</div>
<?php } else { ?>
<div class="post row">
    <div class="col-sm-12">
        <img src="<?php echo $GemstonesImagesDir;?>/<?php echo $row->gem_gemstone_picture_b; ?>" class="img-responsive" alt="" />
        </div>
    </div>