2011-05-01 69 views
2

我有這個php函數,我創建的基本上是一個switch語句。對於每種情況,$ team_image變量都被保存爲一個不同的值。它看起來有點像這樣:PHP函數不會保存變量

function teamImage($team) 
{ 
    switch($team) 
    { 
     case "Baltimore Orioles": 

      $team_image = "orioles"; 

     case "New York Yankees": 

      $team_image = "yankees"; 

     case "Toronto Blue Jays": 

      $team_image = "bluejays"; 

你明白了。但是,當我調用函數並嘗試在我的代碼的其他部分使用$ team_image變量時,它不起作用,因爲顯然變量仍未定義。有什麼想法嗎?

感謝,

蘭斯

+1

沒有足夠的信息來充分回答這個問題。請發佈你打算如何使用'$ team_image'變量。 – 2011-05-01 19:15:21

+2

雖然這似乎不會影響您的問題,但請記住[break語句](http://php.net/break/),如果您需要它,看起來您會像。 – waiwai933 2011-05-01 19:18:24

+0

我不同意沒有足夠的信息來回答問題。我清楚問題出在哪裏(由於缺乏範圍意識)。 – Hammerite 2011-05-01 19:22:02

回答

3

當你只設置$team_imageteamImage函數內部,它只會與有該功能的「scope」。 (通常,變量等將始終存在於儘可能狹窄的範圍內,這在封裝方面是很好的(封裝是object orientated programming的一個關鍵優勢,等等,您可以在學習更多時繼續發現它們)。 )

這樣,則應該從teamImage函數返回$team_image值並將其設置爲如下:

function teamImage($team) { 

    $team_image = NULL; 

    switch($team) { 
     ... 
    }  

    return $team_image; 
} 

$team_image = teamImage($team); 

另一種方法是,以限定teamImage函數內的$team_image變量作爲全局通過添加行global $team_image;在功能的開始,但這不被認爲是良好的做法。

此外,您應該在您的switch語句中將每個case code block設置爲break;,否則您將最終使用最終情況中指定的值設置$team_image。 (即:如果您沒有違反每條聲明,則代碼流將繼續進行下一步。)有關完整詳細信息,請參閱switch PHP manual page

1

這是因爲$ team_image變量的作用域的功能。無論是在功能的開頭聲明$ team_image全球:

function teamImage($team) 
{ 
    global $team_image; 
    ... 

或更好,回報$ team_image在你的函數結束,並將其分配給另一個變量在你需要它:

function teamImage($team) { 
    ... 
    return $team_image 
} 

... 

$image = teamImage($team_name); 
+0

其他海報是正確的,你需要在每個案例陳述後添加一箇中斷。 – Thilo 2011-05-01 19:20:51

1

幾個事實:

  • 你已經忘了break;
  • $team_image有局部範圍
  • 難道你真的不想使用default

答:

你在你的函數中使用​​,如果您還未使用,否則問題可以在$team_imagescope

例子:

事情發生了變化:

新增突破

代碼:

function teamImage($team) 
{ 
    $team_image = ''; 
    switch($team) 
    { 
     case "Baltimore Orioles":  
      $team_image = "orioles"; 
     break; 

     case "New York Yankees":  
      $team_image = "yankees"; 
     break; 

     case "Toronto Blue Jays":  
      $team_image = "bluejays"; 
     break; 
    } 
    return $team_image; 
} 

用法:

$team = 'new York Yankees'; 
$teamImage = teamImage($team); // yankees 
0

此問題是可變範圍之一。 PHP函數有它們自己的符號表,當你在你的函數中分配給變量$team_image時,你真的分配給一個局部變量。該變量在函數結束時超出了範圍,意味着它不再存在。

解決此問題的最佳方法可能是返回函數的值並使用函數調用分配給$team_image變量。

function teamImage($team) 
{ 
    switch($team) 
    { 
     case "Baltimore Orioles": 

      return "orioles"; 

     case "New York Yankees": 

      return "yankees"; 

     case "Toronto Blue Jays": 

      return "bluejays"; 
    } 
} 

$team_image = teamImage($team); 

現在變量$team_image位於您調用函數的範圍內。

如果您希望變量在所有範圍內均可見,您可以使用$GLOBALS['team_image']來代替,但全局變量被廣泛認爲是不好的做法。 (你可以在網上找到很多資源,這將解釋爲什麼。)

0
However, when I call on the function and try to use the $team_image variable in other parts of my code 

你需要在功能

,所以它看起來像這樣

function getTeamImage($team) 
{ 
switch($team) 
{ 
case "a": 
    $team_image = "asdas"; 
    break; 
    #end so on 
} 

return $team_image; 
} 

#And than you can use in your other code: 

$team_image = getTeamImage($team); 
0

第一關的最後返回您$team_image ,您需要在您的交換機中使用break以防止fall-through

http://codepad.org/CVzLAUr0

<?php 

function teamImage($team) 
{ 
    switch($team) 
    { 
     case "Baltimore Orioles": 
      $team_image = "orioles"; 
      break; 
     case "New York Yankees": 
      $team_image = "yankees"; 
      break; 
     case "Toronto Blue Jays": 
      $team_image = "bluejays"; 
      break; 
     default: 
      $team_image = "none"; 
    } 
    return $team_image; 
} 

echo teamImage('Baltimore Orioles'); 

?> 

第二,如果你想使用在全球範圍內修改的變量,你需要使用global關鍵字在函數中,或者使用$GLOBALS陣列:

http://codepad.org/nkT5FxrH

<?php 

$team_image = ''; 

function teamImage($team) 
{ 
    global $team_image; 
    switch($team) 
    { 
     case "Baltimore Orioles": 
      $team_image = "orioles"; 
      break; 
     case "New York Yankees": 
      $team_image = "yankees"; 
      break; 
     case "Toronto Blue Jays": 
      $team_image = "bluejays"; 
      break; 
     default: 
      $team_image = "none"; 
    } 
    return $team_image; 
} 

teamImage('Baltimore Orioles'); 

echo $team_image; 

?>