3

I am a newbie and have written some code in PHP which gives me above mentioned notice. My program works perfectly but i want to resolve the notice. I know i can turn off the notice, but i dont want to do that. Please let me know how can i fix it.

My code -

$GLOBALS['config'] = array(
    'mysql' =>  array(
        'host'  =>  '127.0.0.1',
        'username'  =>  'root',
        'password'  =>  '',
        'database'  =>  'app'
    )
);


            $config = $GLOBALS['config'];
            $path = explode('/', 'mysql/host');

            foreach($path as $bit){                
                if(isset($config[$bit])){
                    $config = $config[$bit];
                }
            }

            echo $config;

My output from

echo $config;

is 127.0.0.1 and it is as expected.

But i am getting the notice on line -

$config = $GLOBALS['config'];

Please help.

4
  • Why are you using $GLOBALS tho? Normally, you use that inside a function to get a variable outside the function scope. I don't see any functions in your example tho. Commented Oct 27, 2013 at 2:12
  • Which line is line 10? Commented Oct 27, 2013 at 2:25
  • Why do people ever use $GLOBALS? If you want to access a global variable inside a function, use a global $config; declaration. Commented Oct 27, 2013 at 2:28
  • why are you declaring $config as an array, then later changing it to a string? That sort of coding is just inviting problems Commented Oct 27, 2013 at 2:47

1 Answer 1

1

Try adding this line right before the $config = $GLOBALS['config']

$config = array();
Sign up to request clarification or add additional context in comments.

2 Comments

Glad it worked. You can choose this as the answer by clicking on the checkmark underneath the number with the arrows.
I will, once I have 15 reputation :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.