dqhsv0147421 2014-12-12 13:32
浏览 71

未定义的变量 - 如何修复?

This is the code I have:

in the php file:

if($special>0){
    $lease_price = (($special/1000)*38);
} else {
    $lease_price = (($price/1000)*38);
}
$lease_price = $this->currency->format($lease_price);

and in the front end tpl file:

<p>
   <i class="fa fa-chevron-down"></i>
   <b>Lease To Buy Price:</b>
   <span><?php if($price>500){ ?>
       <?php echo $lease_price; ?>
   <?php } else { echo 'NA'; } ?></span>                     
</p>

At the moment I'm getting this issue after var dump: Notice: Undefined variable: lease_price in /var/www/framec.co.uk/catalog/view/theme/lexus_superstore/template/product/product.tpl on line 160NULL

Here's a link to a pastebin of my php file http://pastebin.com/bTPtvgUQ

What do I need to add to make this work? Do I need some sort of code like this?:

'type' => $option['type'],

Here's a link to the page this is on http://www.framec.co.uk/index.php?route=product/product&path=59_78&product_id=45

  • 写回答

2条回答 默认 最新

  • dongxing2710 2014-12-12 13:38
    关注

    There are two ways to handle this.

    Initialise it:

    So it's defined and PHP won't return an error when you try to use it.

    $lease_price = '';
    
    if($special>0){
        $lease_price = (($special/1000)*38);
    } else {
        $lease_price = (($price/1000)*38);
    }
    $lease_price = $this->currency->format($lease_price);
    

    Don't use it if not set:

    Your code which tries to access the undefined variable won't be accessed if it's not defined.

    if ( isset($lease_price) ) {
    
        if($special>0){
          $lease_price = (($special/1000)*38);
        } else {
          $lease_price = (($price/1000)*38);
        }
      $lease_price = $this->currency->format($lease_price);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?