我创建了这个php函数,它基本上是一个switch语句。对于每种情况,$team_image变量都保存为不同的值。它看起来有点像这样:
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变量时,它不起作用,因为显然,该变量仍然未定义。有什么想法吗?
谢谢,
Lance
由于您仅在teamImage
函数内设置$team_image
,因此它将仅与该函数的"作用域"一起存在。(一般来说,变量等总是存在于尽可能窄的范围内,这在封装方面是好的。(封装是面向对象编程等的一个关键好处,您可以在了解更多信息时继续发现。(
因此,您应该从teamImage
函数返回$team_image
值,并将其设置如下:
function teamImage($team) {
$team_image = NULL;
switch($team) {
...
}
return $team_image;
}
$team_image = teamImage($team);
另一种选择是通过在teamImage
函数的开头添加行global $team_image;
,将该函数中的$team_image
变量定义为全局变量,但这不是一种好的做法。
此外,您应该在switch语句中为每个case代码块break;
,否则您最终只会使用在最后一个case中分配的值设置$team_image
。(即:如果您不中断每个语句,代码流将继续到下一个。(有关详细信息,请参阅switch PHP手册页。
这是因为$team_image变量的作用域是函数。在函数开头声明$team_image为全局:
function teamImage($team)
{
global $team_image;
...
或者更好的方法是,在函数末尾返回$team_image,并将其分配给另一个需要它的变量:
function teamImage($team) {
...
return $team_image
}
...
$image = teamImage($team_name);
少数事实:
- 你忘了
break;
-
$team_image
具有本地作用域 - 你真的不想使用
default
吗
答案:
您必须在函数中使用return
,如果您尚未使用,则问题可能在$team_image
范围内。
示例:
情况发生了变化:
$team_image
范围- 在switch语句中添加了break
- 完成的功能代码(添加返回(
代码:
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
您需要查看变量范围文档。。变量范围
这个问题是变量范围中的一个。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']
,但全局变量被广泛认为是不好的做法。(你可以在网上找到很多解释原因的来源。(
However, when I call on the function and try to use the $team_image variable in other parts of my code
你需要在功能结束时返回你的$team_image
所以它看起来像这个
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);
首先,您需要在交换机中使用break
来防止故障:
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;
?>