0

我需要一些函数或解决方案来保存函数中变量的值,直到下一次调用函数而不连接到 MySQL。

function test($price){

    if(isset($lastPrice)){
          if($lastPrice>$price){
                    return true;
              }
          $lastPrice = $price; 
         }else{
               $lastPrice = $price;
               returne false;
     }
}

$prices= array ( '1' => '2000',
                 '2' => '2100',
         '3' => '2100'
);

foreach($prices as $key = > $price){

    if($this->test($price)){
         echo 'got expensive';
    }
}

在这段代码中,我需要保存 $lastPrice 直到下一次 test() 在 foreach 中被调用,...

4

2 回答 2

2
function test($price){ () {
  static $lastPrice;
  ...
于 2013-09-28T20:32:38.650 回答
0

试试这个:

$lastPrice = 0;//declaring as global
function test($price){

    if($lastPrice != 0){
          if($lastPrice>$price){
                    return true;
              }
          $lastPrice = $price; 
         }else{
               $lastPrice = $price;
               returne false;
     }
}

$prices= array ( '1' => '2000',
                 '2' => '2100',
         '3' => '2100'
);

foreach($prices as $key = > $price){

    if($this->test($price)){
         echo 'got expensive';
    }
}
于 2013-09-28T20:32:16.507 回答