0

I am making a points system. I made an array that holds the points needed to level up, but it does not work.

Here is the code:

    $level_stats = array();

    $level_stats[1] = 50;

    $level_stats[2] = 100;

    $level_stats[3] = 175;

    $level_stats[4] = 250;

    $level_stats[5] = 350;

    $level_stats[6] = 500;

    $level_stats[7] = 700;

    $level_stats[8] = 950;

    $level_stats[9] = 1250;

    $level_stats[10] = 2000;

    function getLevelRequirementForUser($name){ // function to get needed points to level up
            $level = tonumber(getLevelForUser($name)); // returns 1 (function not shown in code, it works though)
            return $level_stats[$level]; // returns nothing.

    }
  • Wow, three answers, all the same, all with -1 score - why? – Tigger Jan 27 '13 at 05:37
  • 3
    Someone hates global variables, would be my guess – Eva Jan 27 '13 at 05:37
  • can u check with if($level == 1) and see if it goes in.. – Raghavan Jan 27 '13 at 05:43
  • 1
    @Johnny Please do not take the *easy* way out and use global variables. See [this question](http://stackoverflow.com/questions/5166087/php-global-in-functions) and subsequent answers for *very good* reasons why globals are bad – Phil Jan 27 '13 at 05:44

3 Answers3

1

First of all, your array is defined outside the scope of the function. When you try to access the $level_stats inside the function, it is accessing a local variable, which is undefined in your case. That is why it is not returning anything.

You can solve this by either using the global keyword, or passing the array as a parameter to the function.

1)

     function getLevelRequirementForUser($level_stats, $name){ // function to get needed points to level up

        $level = tonumber(getLevelForUser($name)); // returns 1 (function not shown in code, it works though)
        return $level_stats[$level]; // returns nothing.

      }

2)

     function getLevelRequirementForUser($name){ // function to get needed points to level up
        global $level_stats;
        $level = tonumber(getLevelForUser($name)); // returns 1 (function not shown in code, it works though)
        return $level_stats[$level]; // returns nothing.

}
janenz00
  • 3,315
  • 5
  • 28
  • 37
  • 1
    `global` is never an appropriate answer. +1 for option #1 though – Phil Jan 27 '13 at 05:38
  • Thank you for the explanation behind it as well, it works! Thanks! :D – Jonny Barry Jan 27 '13 at 05:38
  • @Phil: Considering the very basic level of understanding of programming that Jonny appears to have, I don't think that it's an unreasonable answer. There are plenty of older PHP apps that use global, he should at least be familiar with it. – Eva Jan 27 '13 at 05:40
  • @Phil, personally I do not like using globals. Still, I guess it all depends on the level of refactoring he needs to do. For a fresh code, #1 it is. – janenz00 Jan 27 '13 at 05:42
  • @Evan using `global` just introduces further scoping hell. See http://stackoverflow.com/questions/5166087/php-global-in-functions – Phil Jan 27 '13 at 05:42
  • @Phil: I'm aware of that, but it's still a correct answer for the problem presented. In my opinion, a singleton class with static variables might be a better answer, but that doesn't mean that using global variables is a wrong answer, it's just an answer that could possible lead to more problems (but it by no means has to, if the code is written properly) – Eva Jan 27 '13 at 05:45
  • @Evan I'd much rather *educate* than simply provide a *working answer* – Phil Jan 27 '13 at 05:48
  • @Phil I think your heart is in the right place, but global variables are used in many existing PHP scripts, and thus it's also necessary to educate on how they can be used. Do you really believe that he'll never run into inferior PHP code? – Eva Jan 27 '13 at 05:52
  • @Evan I can only hope so. I also hope he doesn't write any (bad code) for future developers to stumble across. Trying to work with and debug bad code is a nightmare so why encourage producing more rubbish? – Phil Jan 27 '13 at 05:55
  • @Phil there will always be bad code, I'm just taking the pragmatic viewpoint. I stumble across bad code almost every day. – Eva Jan 27 '13 at 05:58
1

This appears to be a simple scoping problem. The $level_stats array is not in the function's scope.

If you don't need $level_stats anywhere else, you could simply define it within your function, eg

function getLevelRequirementForUser($name) {
    $level_stats = array(
        1 => 50,
        2 => 100,
        // etc
    );

    $level = tonumber(getLevelForUser($name));
    return $level_stats[$level];
}
Phil
  • 157,677
  • 23
  • 242
  • 245
0

You need to globalize your variable:

function getLevelRequirementForUser($name){ 
        global $level_stats;

        $level = tonumber(getLevelForUser($name));
        return $level_stats[$level];
}
Liftoff
  • 24,717
  • 13
  • 66
  • 119