0

Hi i am having this structure:

$GLOBALS['config'] = array(
            'mysql' => array(
                    'host' => 'localhost',
                    'username' => 'root',
                    'password' => 'root',
                    'dbname' => 'database'
                ),
            'session' => array(
                    'session_name' => 'user'
                ),
            'remember' => array(
                    'cookie_name' => 'hash',
                    'cookie_expiry' => 604800
                ),
            'folder' => array(
                    'root' => 'backend',
                    'header' => 'head',
                    'views' => 'views'
                ),
            'database' => array(
                    'names' => 'utf8mb4',
                    'charset' => 'utf8mb4',
                    'collation' => 'utf8mb4_general_ci',
                    'driver' => 'pdo'
                ),
            'url' => array(
                'base_url' => 'http://www.example.com/backend/',
                'document_root' => $_SERVER['DOCUMENT_ROOT'] . "/backend"
                ),
            'languages' => array(
                    'english' => 'en',
                    'german' => 'de',
                    'greek' => 'gr'
                ),
            'headers' => array(
                    '404' => 'HTTP/1.0 404 Not Found',
                    '401' => 'HTTP/1.0 401 Unauthorized',
                    '500' => 'HTTP/1.0 500 Internal Server Error',
                    '403' => 'HTTP/1.0 403 Forbidden'
                ),
            'title' => array(
                    'login' => 'Admin Dashboard',
                    'register' => 'Admin Dashboard | User Registration',
                )
        );

and i want in the url/base_url to be like this

'base_url' => 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/'

so if i change folder i only have to change the name in only place, but i get a Syntax error like:

Notice: Undefined index: config in C:\xampp-php56\htdocs\backend\core\init.php on line 31

Is possible what i am trying to do? and if possible how?

4
  • You can't reference an array's index within its own array definition Commented Jun 9, 2016 at 11:55
  • any idea how can i reference to the folder array within this url array? Commented Jun 9, 2016 at 11:56
  • Just set it after you've defined the array. Commented Jun 9, 2016 at 11:57
  • @BRG see my answer below Commented Jun 9, 2016 at 11:57

2 Answers 2

2

You can't access another array index while you're still defining the array. Your statement defining the array hasn't completed yet, the array isn't available for access until the initial statement is finished.

You need to setup your array as much as you can first, and then go back and add the array elements that reference other array indexes.

So first just create your big array like you're doing, without the base_url.

$GLOBALS['config'] = array(
    ...
);

Now go back and add the url/base_url, you can now access the array index of config.

$GLOBALS['config']['url']['base_url'] = 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/';

Example: https://3v4l.org/M7itf

Sign up to request clarification or add additional context in comments.

2 Comments

@BRG I don't think you understand. You can't access an array index while you're in the middle of defining the array. You can't do what you're trying to do. I'm showing you the proper way to accomplish this, even if it's not what you wanted.
@BRG see the example link I just included
1

Missed dot 'base_url' => 'http://www.example.com/'.$GLOBALS['config']['folder']['root'].'/'

1 Comment

and now it says undefined index config...i guess it cannot see that the folder is named config

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.