2011-05-01 62 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; 

?>