0

This is really weird. I know what Undefined index means. But look at this code.

This is what I got on the top of the script:

$__load = array(
    'loss_experience' => NULL,
    'loss_items' => NULL,
    'guild_info' => NULL,
    'skull_type' => NULL,
    'skull_time' => NULL,
    'blessings' => NULL,
    'direction' => NULL,
    'stamina' => NULL,
    'world_id' => NULL,
    'online' => NULL,
    'deletion' => NULL,
    'promotion' => NULL,
    'marriage' => NULL
);

then in class I got this:

   public function load($id)
    {
        global $__load;

        if($__load['loss_experience'] == NULL)
        {
            $loss = '';
            if(fieldExist('loss_experience', 'players')) {
                $loss = ', `loss_experience`, `loss_mana`, `loss_skills`';
            }

            $__load['loss_experience'] = $loss;
        }

        if($__load['loss_items'] == NULL)
        {
            $loss_items = '';
            if(fieldExist('loss_items', 'players')) {
                $loss_items = ', `loss_items`, `loss_containers`';
            }

            $__load['loss_items'] = $loss_items;
        }

and this is error I become:

Notice: Undefined index: loss_items in D:\xampp\htdocs\myaac\system\libs\pot\OTS_Player.php on line 145

Line 145 is this:

if($__load['loss_items'] == NULL)

What I did wrong?

When I comment this line:

$__load['loss_experience'] = $loss;

Then Notice is not showed. But I need to declare it.

@Edit I've tried this:

    public function load($id)
    {
        global $__load;
var_dump($__load);
        if($__load['loss_experience'] == NULL)
        {
            $loss = '';
            if(fieldExist('loss_experience', 'players')) {
                $loss = ', `loss_experience`, `loss_mana`, `loss_skills`';
            }

            $__load['loss_experience'] = $loss;
        }
var_dump($__load);
        if($__load['loss_items'] == NULL)
        {

Output of first var_dump is: NULL

And second var_dump: array(1) { ["loss_experience"]=> string(0) "" }

So it seems it doesn't see my top declared $__load variable.. but why?

slawkens
  • 138
  • 7
  • Tested, works just fine on my side. Try doing `var_dump($__load);` – Ibu May 18 '17 at 18:15
  • You are declaring your function public -- is this a class method? If so, why not make `$__load` a private variable and instead use `$this->__load`? – Jeremy Harris May 18 '17 at 18:28
  • I've edited my post. I tried what you said and it seems it doesn't see my top declared variable. – slawkens May 18 '17 at 18:28
  • @JeremyHarris I tried, but I make more instances of this class, and I need this variable to be global. I tried also defining it as static variable in class, but then I got this error: "Can't use method return value in write context" – slawkens May 18 '17 at 18:31
  • In your function what does `print_r($GLOBALS)` show? – AbraCadaver May 18 '17 at 18:32
  • @AbraCadaver on the first run it showed empty array. But on the next run it wasn't empty. Anyway, i fixed it already. – slawkens May 18 '17 at 18:48

1 Answers1

1

'Undefined index' means you haven't stored a value in that element of your associative array '__load'. You didn't really define it when setting it to null.

Instead of testing for == null, use !isset() .

It is just a warning, so it's not breaking your code.

vanoden
  • 26
  • 2