PHP Switch案例& if语句没有返回正确的值

时间:2015-12-31 08:25:48

标签: php

我是PHP的新手,我正在尝试编写一个在用户输入值后计算点数的脚本。

以下是代码:

<?php

$pla = $_GET['players'];
$plu = $_GET['plugins'];
$type = $_GET['type'];
$location = $_GET['location'];
totalPoints();

function typePoints($type) { //Returns points depending on server type
    switch (strtolower($type)) { //Switch the type of server between all types and see how many points to return :)
        case "standard minecraft": return 1;
            break;
        case "snapshot": return 2;
            break;
        case "craftbukkit": return 2;
            break;
        case "bungeecord": return 1;
            break;
        case "spigot": return 2;
            break;
        case "paperspigot": return 2;
            break;
        case "feed the beast": return 3;
            break;
        case "technic": return 3;
            break;
        case "pixelmon": return 3;
            break;
        default: echo 'Incorrect Server Type!';
            exit;
            break;
    }
}

function playerPoints($players) { //Returns points depending on amount o players
    if ($players >= 2 && $players <= 5) {
        return 2;
    } elseif ($players >= 6 && $players <= 10) {
        return 4;
    } //Between 6-10, return 4 points... AND SO ON...
    elseif ($players >= 11 && $players <= 16) {
        return 8;
    } elseif ($players >= 17 && $players <= 25) {
        return 12;
    } elseif ($players >= 26 && $players <= 30) {
        return 16;
    } elseif ($players >= 31 && $players <= 36) {
        return 18;
    } elseif ($players >= 37 && $players <= 45) {
        return 20;
    } elseif ($players >= 46 && $players <= 50) {
        return 22;
    } elseif ($players >= 51 && $players <= 56) {
        return 24;
    } elseif ($players >= 57 && $players <= 65) {
        return 26;
    } elseif ($players >= 66 && $players <= 70) {
        return 28;
    } elseif ($players >= 71 && $players <= 76) {
        return 30;
    } elseif ($players >= 77 && $players <= 85) {
        return 32;
    } elseif ($players >= 86 && $players <= 90) {
        return 34;
    } elseif ($players >= 91 && $players <= 96) {
        return 36;
    } elseif ($players >= 97 && $players <= 105) {
        return 38;
    } elseif ($players >= 106 && $players <= 110) {
        return 40;
    } elseif ($players > 110) {
        return 44;
    }
}

function pluginPoints($plugins) {
    if ($plugins == 0) {
        return 0;
    } elseif ($plugins >= 1 && $plugins <= 3) {
        return 2;
    } //Between 1-3, return 2 points.... AND SO ON...
    elseif ($plugins >= 4 && $plugins <= 15) {
        return 6;
    } elseif ($plugins >= 16 && $plugins <= 30) {
        return 10;
    } elseif ($plugins >= 31 && $plugins <= 40) {
        return 14;
    } elseif ($plugins >= 41 && $plugins <= 50) {
        return 20;
    } elseif ($plugins > 50) {
        return 24;
    }
}

function locationPoints($location) {
    switch (strtolower($location)) { //Switch between locations, easy to add a new one later in the future. :)
        case "montreal": return 1;
            break;
        case "france": return 1;
            break;
        default: echo "Incorrect Location!";
            exit;
            break;
    }
}

function totalPoints() { //Call this function to get the amount of points finally.
    $totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
    echo 'Total points: ' . $totalPoints;
}

?>

问题是switch中的function typePoints($type)语句始终返回default: echo 'Incorrect Server Type!'; exit; break;

此外,即使我输入function playerPoints($players)之类的数字,0也会返回37

我用过这个:

  

http://website.com/planpicker.php?players=121&location=france&plugins=80&type=technic

谁能告诉我为什么?

这不是一个重复的问题,“标记的重复”讨论了不同的文件,但这是在同一个文件中。

3 个答案:

答案 0 :(得分:2)

totalPoints函数无法访问4个变量。您应该将这些参数传递给函数。您应该将函数调用为:

totalPoints($type,$pla,$plu,$location);

将函数定义为

function totalPoints($type,$pla,$plu,$location)

答案 1 :(得分:0)

这是因为你在另一个函数中调用函数而不传递参数中的值。

totalPoints函数不知道$ type或$ pla的值,例如因为它们不是GLOBALS。

您必须将变量传递给您要调用的函数:

function totalPoints($type, $pla, $plu, $location) //Call this function to get the amount of points finally.
{
    $totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
    echo 'Total points: '.$totalPoints;
}

然后调用这个函数:

totalPoints($type,$pla,$plu,$location);

答案 2 :(得分:0)

需要使totalPoints函数可以访问函数外部已解除的变量。最简单的方法是使用global

 function totalPoints() {
  global $pla;
  global $plu;
  global $type;
  global $location;

  $totalPoints = typePoints($type) + playerPoints($pla) + pluginPoints($plu) + locationPoints($location);
  echo 'Total points: '.$totalPoints;

}