2013-07-23 64 views
-1

的問題是要改變全局變量的值,每次子叫,我怎麼能做到這一點如何讓靜態變量初始化爲全局變量

  function sub() 
      { 
      static $x=global $present; 
      static $y=global $max; 
      static $z=global $min; 
     if($x==$z) 
     { 
    $x=$y; 
      } 
      $x--; 

      return $x; 
       } 

      sub(); 
      sub(); 
       sub(); 

我曾經嘗試這樣做太

  function sub() 
      { 
      global $present; 
      global $max; 
      global $min; 
     if($present==$min) 
     { 
    $present=$max; 
      } 
      $present--; 

      return $present; 
       } 

       sub();//it works only once 
       sub(); 
       sub(); 

,請提供給我的解決方案,這樣我可以改變全局變量每次調用函數的值.......謝謝

子的主要功能是檢索值FRM數據基地,但仍然SRC相同,無論多少次,我呼叫改變功能,請幫我出

  function sub() 
       { 
global $present; 
global $max; 
global $min; 
     if($present==$min) 
     { 
$present=$max; 
      } 
      else 
      --$present; 
     return $present; 
       } 


       <script> 
      function change() 
        { 

        alert("hello"); 
       var x=document.getElementById("show"); 
     x.src='<?php 



     if($con==true) 
      { 

     $cmd="select * from showcase where item_no=".sub(); 
      if($res=$con->query($cmd)) 
       { 
     if($res->num_rows>0) 
      { 
      while($rw=$res->fetch_array()) 
      { 

      echo "$rw[1]"; 
       } 
       } 
    else 
     { 
    echo "no record found"; 
     } 
     } 
     else 
    { 
     echo "query problem"; 
     }} 

      ?>'; 
      alert(x.src); 
        } 
        </script> 
+0

你有沒有聽說過[indentation](http://en.wikipedia.org/wiki/Indentation)? –

+0

沒有讓你對不起? – user2558479

+0

@Maerlyn我已經把實際使用...現在除了我的qustn請現在看到 – user2558479

回答

1

此代碼的工作對我來說:

<?php 

$x = 5; 

function sub() { 
    global $x; 

    --$x; 
} 

sub(); var_dump($x); 
sub(); var_dump($x); 
sub(); var_dump($x); 
sub(); var_dump($x); 

輸出爲4,3,2,1。請參閱爲自己:http://3v4l.org/Tic1v

+0

我已經把代碼的使用,現在請解決我的問題 – user2558479

+0

你是可怕的混合客戶端和服務器端代碼。這不能簡單地修復。 – Maerlyn