-1

I am using PHP 5.6 and have the following code:

$page->adminNavi[$i]->active    = _SITE == $file || _ACTIV_NAVI == $key ? true : false;

On the above line, I am receiving the following error:

Creating default object from empty value in

How can I fix this error?

honk
  • 9,137
  • 11
  • 75
  • 83
SusanT
  • 1
  • 1

1 Answers1

1

The error is from this:

$page->adminNavi[$i]->active 

Either $page isn't set or an object, or adminNavi isn't an array, or adminNavi[$i] doesn't exist, or isn't a stdClass.

Debug it!

var_dump($page->adminNavi); exit;

With luck, you'll get an array. In which case var dump array key $i and see what's in there.

UPDATE: okay so the var dump returns this

array(1) { [0]=> object(stdClass)#2 (1) { ["active"]=> bool(false) } } 

How many times does $i change? If it's anything other than 0, that array key will not exist, but you immediately refer to it like it does, and since you treat it like a stdClass, it creates one on the fly but generates the warning.

To sum up, make sure $i exists by counting the array! If $i is set from a loop, then something like this:

for ($i = 0; $i <= count($page->adminNavi): $i++) {
    // your code
}
delboy1978uk
  • 12,118
  • 2
  • 21
  • 39