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

5
  • 1
    And what is the error? Commented Oct 22, 2018 at 14:25
  • are _SITE and _ACTIV_NAVI defined constants? Maybe it should be _ACTIVE_NAVI? Commented Oct 22, 2018 at 14:26
  • 1
    You forgot to provide the error. Commented Oct 22, 2018 at 14:28
  • You dont need the ternary _ACTIV_NAVI == $key ? true : false;. On it's own, _ACTIV_NAVI == $key returns true or false already Commented Oct 22, 2018 at 14:29
  • Either $page isn't an object, or $page->adminNavi[$i] isn't an object. Debug your code. Commented Oct 22, 2018 at 14:30

1 Answer 1

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

array(1) { [0]=> object(stdClass)#2 (1) { ["active"]=> bool(false) } }
$page->adminNavi
updated have a look

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.