dpde7365 2011-05-09 04:15
浏览 42
已采纳

PHP函数不会保存变量

I have a function that looks like this:

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

        $team_home[42] = "Tampa Bay Rays";
        $team_home[43] = "Boston Red Sox";
        $team_home[44] = "Boston Red Sox";
        $team_home[45] = "$team";
        $team_home[46] = "$team";
        $team_home[47] = "$team";
        $team_home[48] = "$team";

        $team_away[42] = "$team";
        $team_away[43] = "$team";
        $team_away[44] = "$team";
        $team_away[45] = "New York Yankees";
        $team_away[46] = "New York Yankees";
        $team_away[47] = "Washington Nationals";
        $team_away[48] = "Washington Nationals";

        $team_date[42] = "Sun, May 15";
        $team_date[43] = "Mon, May 16";
        $team_date[44] = "Tue, May 17";
        $team_date[45] = "Wed, May 18";
        $team_date[46] = "Thu, May 19";
        $team_date[47] = "Fri, May 20";
        $team_date[48] = "Sat, May 21";

        break;

        case "Boston Red Sox":

        $team_home[42] = "$team";
        $team_home[43] = "$team";
        $team_home[44] = "$team";
        $team_home[45] = "$team";
        $team_home[46] = "$team";
        $team_home[47] = "$team";
        $team_home[48] = "$team";

        $team_away[42] = "Baltimore Orioles";
        $team_away[43] = "Baltimore Orioles";
        $team_away[44] = "Detroit Tigers";
        $team_away[45] = "Detroit Tigers";
        $team_away[46] = "Chicago Cubs";
        $team_away[47] = "Chicago Cubs";
        $team_away[48] = "Chicago Cubs";

        $team_date[42] = "Mon, May 16";
        $team_date[43] = "Tue, May 17";
        $team_date[44] = "Wed, May 18";
        $team_date[45] = "Thu, May 19";
        $team_date[46] = "Fri, May 20";
        $team_date[47] = "Sat, May 21";
        $team_date[48] = "Sun, May 22";

        break;
  }

  for($i = 42;$i < 49;++$i)
  {
    return $team_home[$i];
    return $team_away[$i];
    return $team_date[$i];
  }

When I try to use the $team_date, $team_away, and $team_home variables as follows, the only one that seems to work is the $team_home variable.

$game = filter_input(INPUT_GET, 'game', FILTER_SANITIZE_STRING);

$team_home[$game] = findSchedule($team);
$team_away[$game] = findSchedule($team);
$team_date[$game] = findSchedule($team);

Any ideas?

Thanks,

Lance

  • 写回答

2条回答 默认 最新

  • dpwdsmbvm496180204 2011-05-09 04:19
    关注

    It has to do with how you constructed your final for loop. You have it set to return each value; the return statement ends processing. You want to echo each one and then return, or alternatively never explicitly state return but just end the function.

    To return all arguments, you could just make a single, larger array and return that:

    return array( $team_home, $team_away, $team_date);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 自己做的代码上传图片时,报错
  • ¥15 Lingo线性规划模型怎么搭建
  • ¥15 关于#python#的问题,请各位专家解答!区间型正向化
  • ¥15 unity从3D升级到urp管线,打包ab包后,材质全部变紫色
  • ¥50 comsol温度场仿真无法模拟微米级激光光斑
  • ¥15 上传图片时提交的存储类型
  • ¥15 VB.NET如何绘制倾斜的椭圆
  • ¥15 arbotix没有/cmd_vel话题
  • ¥15 odoo17的分包重新供应路线如何设置?可从销售订单中实时直接触发采购订单或相关单据
  • ¥15 用C语言怎么判断字符串的输入是否符合设定?