PHP函数不会保存变量
我创建了这个 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 变量时,它不起作用,因为显然该变量仍然未定义。有什么想法吗?
谢谢,
兰斯
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
由于您仅在
teamImage
函数内设置$team_image
,因此它仅与该函数的“范围"。 (一般来说,变量等总是存在于尽可能窄的范围内,这对于封装来说是很好的。(封装是 面向对象编程等,随着您了解更多,您可能会继续发现。)因此,您应该从
$team_image
返回值code>teamImage 函数并将其设置如下:另一种方法是通过添加行
teamImage
函数将$team_image
变量定义为全局变量code>global $team_image; 在函数的开头,但这不被认为是好的做法,此外,您应该
break;
switch 语句中的每个 case 代码块,否则您会这样做。最终将使用最终案例中分配的值来设置$team_image
(即:如果您不中断每个语句,代码流将继续到下一个。)请参阅 切换 PHP 手册页以获取完整详细信息。As you're only setting the
$team_image
inside theteamImage
function, it will only exist with that function's "scope". (In general, variables, etc. will always exist in as narrow a scope as possible, which is good in terms of encapsulation. (Encapsulation being a key benefit of object orientated programming, etc. which you may go on to discover as you learn more.)As such, you should return the
$team_image
value from theteamImage
function and set it as follows:An alternative would be to define the
$team_image
variable within theteamImage
function as a global by adding the lineglobal $team_image;
at the beginning of the function, but this isn't considered good practice.Additionally, you should
break;
each case code block within your switch statement as otherwise you'll simply end up setting$team_image
with the value assigned in your final case. (i.e.: If you don't break each statement, code flow will continue into the next.) See the switch PHP manual page for full details.这是因为 $team_image 变量的作用域是该函数。要么在函数开头将 $team_image 声明为全局:
或者更好的是,在函数末尾返回 $team_image 并将其分配给您需要的另一个变量:
That's because the $team_image variable is scoped to the function. Either declare $team_image as global at the beginning of the function:
or better, return $team_image at the end of your function and assign it to another variable where you need it:
一些事实:
break ;
$team_image
具有本地范围默认
吗?答案:
您必须使用
return
在你的函数中,如果你还没有使用过,否则问题可能出在$team_image
范围。示例:
更改内容:
$team_image
范围代码:
用法:
Few facts:
break;
$team_image
have local scopedefault
?Answer:
You have to use
return
in your function, if you haven't used already, otherwise the problem can be in$team_image
scope.Example:
Things changed:
$team_image
scopeCode:
Usage:
您需要查看变量范围文档..变量范围
you need to see the variables scope documentation..variables scope
这个问题是变量范围之一。 PHP 函数有自己的符号表,当您在函数中分配给变量
$team_image
时,实际上是在分配给局部变量。该变量在函数末尾“超出范围”,这意味着它不再存在。解决此问题的最佳方法可能是从函数中返回值,并使用函数调用分配给
$team_image
变量。现在变量
$team_image
位于您调用该函数的范围内。如果您希望变量在所有范围内都可见,则可以使用 $GLOBALS['team_image'] 代替,但全局变量被广泛认为是不好的做法。 (您可以在网上找到许多可以解释原因的资源。)
This problem is one of variable scope. PHP functions have their own symbol table, and when you assign to the variable
$team_image
in your function, you're really assigning to a local variable. That variable goes 'out of scope' at the end of the function, meaning it doesn't exist any more.The best way to fix this would probably be to return the value from the function and assign to the
$team_image
variable using the function call.Now the variable
$team_image
is in the scope in which you call the function.If you want the variable to be visible in all scopes, you can use
$GLOBALS['team_image']
instead, but global variables are widely considered bad practice. (You can find many sources online that will explain why.)您需要在函数末尾返回您的
$team_image
,因此它看起来像这样
you need to return your
$team_image
at the end of the functionso it looks like this
首先,您需要在开关中使用
break
来防止 fall-through:http://codepad.org/CVzLAUr0
其次,如果你想使用在全局范围内修改的变量,您需要在函数中使用
global
关键字,或使用$GLOBALS
数组:http://codepad.org/nkT5FxrH
First off, you need to use
break
in your switch to prevent fall-through:http://codepad.org/CVzLAUr0
Second, if you want to use a variable that is modified in a global scope, you need to use the
global
keyword within the function, or use the$GLOBALS
array:http://codepad.org/nkT5FxrH