Raghav J.
Raghav J.

Reputation: 53

PHP Switch case & if statement not returning correct values

I'm kind of new to PHP and I'm trying to write a script that counts points after a user inputs values.

Here's the code:

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

?>

The problem is the switch statement in function typePoints($type) always returns the default: echo 'Incorrect Server Type!'; exit; break;.

Also, function playerPoints($players) returns 0 even though I put in a number like 37.

I used this:

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

Can anyone tell me why?

This is not a duplicate question, the "marked duplicate" talks about different files, but this is within the same file.

Upvotes: 3

Views: 90

Answers (3)

AnkiiG
AnkiiG

Reputation: 3488

The 4 variables are not accessible from totalPoints function. You should pass those parameters to function. You should call function as :

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

And define the function as

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

Upvotes: 2

Professor Abronsius
Professor Abronsius

Reputation: 33823

The variables that were decared outside of the functions need to be made accessible to the totalPoints function. The easiest way is to use global

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

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

}

Upvotes: 0

jiboulex
jiboulex

Reputation: 3021

It's because you call your functions in another function without passing the values in parameter.

The totalPoints function is not aware of the value of $type or $pla for example as they are not GLOBALS.

You have to pass the variables to the function you want to call :

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

And then call the function like this :

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

Upvotes: 0

Related Questions