PHP切换问题

时间:2016-08-01 23:16:43

标签: php api switch-statement

我有来自API的输出:

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
                <tr>
                    <td><?php echo $fixture->homeTeamName; ?></td>
                    <td>-</td>
                    <td><?php echo $fixture->awayTeamName; ?></td>
                    <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                    <td>:</td>
                    <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
                </tr>
<?php } ?>

这很好用,它会输出如下名字:Walsall FC in:

<td><?php echo $fixture->homeTeamName; ?></td>

现在我希望将输出转换为FR语言,例如“équiperouge”而不是“Walsall FC”

我在php页面的标题中尝试这个PHP Switch,但是我得到了空白屏幕:

switch ($teamName) {
    case $fixture->homeTeamName['Walsall FC']:
        echo "équipe rouge";
        break;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

您应该将变量放在括号内,将值放在case之后:

switch ($fixture->homeTeamName) {
    case 'redBlackpool FC':
        echo "équipe rouge";
        break;
}

但也许你在模板中加入太多逻辑?也许你可以做到:

switch ($fixture->homeTeamName) {
    case 'redBlackpool FC':
        $translatedHomeTeamName =  "équipe rouge";
        break;
}

然后在模板中:

<td><?php echo $translatedHomeTeamName; ?></td>

如果您无法修改<td>的内容,则可以执行以下操作:

switch ($fixture->homeTeamName) {
    case 'redBlackpool FC':
        $fixture->homeTeamName =  "équipe rouge";
        break;
}

上面应该在打印<td>之前执行...基本上我只是建议你不要把整个switch语句放在<td>里面,这样你就可以保持你的代码整洁...

修改 - 基于已修改的问题

由于您位于foreach循环中,因此您需要在foreach中翻译该术语,如下所示:

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { 

        switch ($fixture->homeTeamName) {
            case 'redBlackpool FC':
                $fixture->homeTeamName =  "équipe rouge";
                break;
            case 'blueBlackpool FC':
                $fixture->homeTeamName =  "équipe bleu";
                break;
            default:
            // here you can do something in case the team isn't any of the above.
        }
?>
            <tr>
                <td><?php echo $fixture->homeTeamName; ?></td>
                <td>-</td>
                <td><?php echo $fixture->awayTeamName; ?></td>
                <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                <td>:</td>
                <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
            </tr>
<?php } ?>

更新 - 功能示例 该函数(您可以将它放在一个单独的文件中,然后将其包含在主脚本中):

function translateTeam($homeTeamName)
    switch ($homeTeamName) {
        case 'redBlackpool FC':
            $homeTeamName =  "équipe rouge";
            break;
        case 'blueBlackpool FC':
            $homeTeamName =  "équipe bleu";
            break;
        default:
        // here you can do something in case the team isn't any of the above.
    }

    return $homeTeamName;
}

foreach循环:

// Include the php script that contains translateTeam() here...

<?php foreach ($soccerseason->getFixturesByMatchday(1) as $fixture) { ?>
            <tr>
                // In the line below we call the function and pass the English name to it...
                <td><?php echo translateTeam($fixture->homeTeamName); ?></td>
                <td>-</td>
                <td><?php echo $fixture->awayTeamName; ?></td>
                <td><?php echo $fixture->result->goalsHomeTeam; ?></td>
                <td>:</td>
                <td><?php echo $fixture->result->goalsAwayTeam; ?></td>
            </tr>
<?php } ?>