PHP函数不会保存变量

发布于 2024-11-04 06:18:04 字数 479 浏览 1 评论 0 原文

我创建了这个 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 变量时,它不起作用,因为显然该变量仍然未定义。有什么想法吗?

谢谢,

兰斯

I have this php function that I created that is basically a switch statement. For each case, the $team_image variable is saved to a different value. It looks a little something like this:

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";

You get the idea. However, when I call on the function and try to use the $team_image variable in other parts of my code, it doesn't work because apparently, the variable is still undefined. any thoughts?

Thanks,

Lance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

や三分注定 2024-11-11 06:18:04

由于您仅在 teamImage 函数内设置 $team_image,因此它仅与该函数的“范围"。 (一般来说,变量等总是存在于尽可能窄的范围内,这对于封装来说是很好的。(封装是 面向对象编程等,随着您了解更多,您可能会继续发现。)

因此,您应该从 $team_image 返回值code>teamImage 函数并将其设置如下:

function teamImage($team) {

    $team_image = NULL;

    switch($team) {
       ...
    }    

    return $team_image;
}

$team_image = teamImage($team);

另一种方法是通过添加行 teamImage 函数将 $team_image 变量定义为全局变量code>global $team_image; 在函数的开头,但这不被认为是好的做法,

此外,您应该 break; switch 语句中的每个 case 代码块,否则您会这样做。最终将使用最终案例中分配的值来设置 $team_image (即:如果您不中断每个语句,代码流将继续到下一个。)请参阅 切换 PHP 手册页以获取完整详细信息。

As you're only setting the $team_image inside the teamImage 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 the teamImage function and set it as follows:

function teamImage($team) {

    $team_image = NULL;

    switch($team) {
       ...
    }    

    return $team_image;
}

$team_image = teamImage($team);

An alternative would be to define the $team_image variable within the teamImage function as a global by adding the line global $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.

神爱温柔 2024-11-11 06:18:04

这是因为 $team_image 变量的作用域是该函数。要么在函数开头将 $team_image 声明为全局:

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

或者更好的是,在函数末尾返回 $team_image 并将其分配给您需要的另一个变量:

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

...

$image = teamImage($team_name);

That's because the $team_image variable is scoped to the function. Either declare $team_image as global at the beginning of the function:

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

or better, return $team_image at the end of your function and assign it to another variable where you need it:

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

...

$image = teamImage($team_name);
还如梦归 2024-11-11 06:18:04

一些事实:

  • 你忘记了break ;
  • $team_image 具有本地范围
  • 您真的不想使用默认吗?

答案:

您必须使用 return 在你的函数中,如果你还没有使用过,否则问题可能出在$team_image 范围

示例:

更改内容:

代码:

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

Few facts:

  • You've forgot break;
  • $team_image have local scope
  • Do you really don't want to use default?

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:

Code:

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;
 }

Usage:

$team = 'new York Yankees';
$teamImage = teamImage($team); // yankees
小女人ら 2024-11-11 06:18:04

您需要查看变量范围文档..变量范围

you need to see the variables scope documentation..variables scope

半仙 2024-11-11 06:18:04

这个问题是变量范围之一。 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'] 代替,但全局变量被广泛认为是不好的做法。 (您可以在网上找到许多可以解释原因的资源。)

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.

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);

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.)

尸血腥色 2024-11-11 06:18:04
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);
However, when I call on the function and try to use the $team_image variable in other parts of my code

you need to return your $team_image at the end of the function

so it looks like this

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);
无人问我粥可暖 2024-11-11 06:18:04

首先,您需要在开关中使用 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;

?>

First off, you need to use break in your switch to prevent 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');

?>

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

<?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;

?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文