0

我正在从某个给我团队名称的地方删除信息。如果我这样做echo $HomeTeam;,我会得到“Man Utd”的价值。

但是当我这样做时..它不起作用(显示空白)。

$PlayerName = "Robin Van Persie"; //just to test that it's working
switch($PlayerName)
    {
    case "Robin Van Persie":
        if ($HomeTeam == "Man Utd") { echo $HomeTeam; } 
        break;
    default: echo "Player not in the list"; break;
    }

这显示为空白...任何想法为什么?我尝试添加$HomeTeam = strval($HomeTeam);以将其转换为字符串,但没有任何区别。

4

4 回答 4

0

如果您要对其进行硬编码而不是将值存储在数据库中,那么这种使用数组的方法可能会让您感兴趣:

$search = "Robin Van Persie";

//Your data array, easyier to add to no
$teams = array(
    'Manchester United'=>array('Robin Van Persie',
                               'Wayne Rooney',
                               ),

    'Arsenal'=>array('Theo Walcott',
                     'Nicklas Bendtner',
                     ),             
);

$result=null;
foreach($teams as $team=>$players) {
    if(in_array($search,$players)) {
        $result = $team;
    }
}
//Robin Van Persie's team is Manchester United
echo ($result != null) ? $search.'\'s team is '.$result : 'Team for '.htmlentities($search).' not found.';
于 2012-11-09T23:23:01.300 回答
0

$HomeTeam variable is not set, that's why it returns empty when printed. set the value to something like this & it should work.

$PlayerName = "Robin Van Persie"; //just to test that it's working
$HomeTeam = "Man Utd";
switch($PlayerName)
    {
    case "Robin Van Persie":
        if ($HomeTeam == "Man Utd") { echo $HomeTeam; } //Man Utd
        break;
    default: echo "Player not in the list"; break;
    }
于 2012-11-09T22:54:02.653 回答
0

尝试这个

$PlayerName = "Robin Van Persie"; //just to test that it's working
switch($PlayerName){
    case "Robin Van Persie":
        $HomeTeam ?  print($HomeTeam) : print("HomeTeam is not set");
    break;
    default: echo "Player not in the list"; break;
}
于 2012-11-09T23:01:57.170 回答
0

if ($HomeTeam == "Man Utd")没有其他事情会发生,所以 $HomeTeam 不能等于"Man Utd"

于 2012-11-09T23:00:32.743 回答