dongshang1934 2013-12-12 13:56
浏览 39
已采纳

使用foreach增加变量

I want to increment a variable with a foreach loop. I save data in a session and for each data piece I make an if statement to check the dimensions and connect it to a price. Then I need to increment these prices in the variable $pr_total_str_saving.

Can someone help me with this?

if(isset($_SESSION['straight_saving'])) : 

    foreach($_SESSION['straight_saving'] as $key => $val) {

        if($val['wz_saving_a'] >= 1 && $val['wz_saving_a'] <= 300) :
            $str_saving_price = 25;
        elseif($val['wz_saving_a'] >= 301 && $val['wz_saving_a'] <= 500) :
            $str_saving_price = 39;
        endif;

        $pr_total_str_saving + $str_saving_price;

    }

    echo $pr_total_str_saving;

endif;
  • 写回答

2条回答 默认 最新

  • doukan1258 2013-12-12 13:59
    关注

    I believe you're looking for the += operator. Here's how you could use it:

    $pr_total_str_saving += $str_saving_price;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?