2013-09-28 79 views
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是保存,直到下一次测试()得到调用的foreach,...

+0

你需要一个全局变量来做到这一点 – Misters

回答

2
function test($price){() { 
    static $lastPrice; 
    ... 
+0

好和简短,... – Phraates

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'; 
    } 
} 
+0

谢谢,但静态是我需要的东西,我不知道,谢谢你的回答... – Phraates

相关问题