Alexander Starbuck
Alexander Starbuck

Reputation: 1159

Code explanation - array of arrays stored in php $GLOBALS variable

I am learning the principles of an OOP based login/logout system (by phpacademy on YT). Here is the code that is giving me headache:

$GLOBALS['config'] = array(
    'init' = array(
        'hostname' = "127.0.0.1",
        'username' = 'root',
        'password' = '',
        'db' = 'dbName'
    ),
    'remember' = array(
        'cookie_name' = 'hash',
        'cookie_expiry' = 604800
    ),
    'session' = array(
        'session_name' = 'user'
    )
);

spl_autoload_register(function($class){
    require_once 'classes/' . $class . '.php';
});

This is obviously an array of arrays, with various bits of info needed to initialize a session and connect to the DB stored in these "sub-arrays".

What I can't wrap my head around is the beginning - he is obviously storing this array of arrays in a $GLOBALS variable; what is the ['config'] part?

  1. regular variable has $variableName (so => "GLOBALS)
  2. regular variable has scope, type and value (so => array();)

What is than thi in-between "[]"? Variable name? Declaration that what follows is an array (why the word array then, after the "="?)

Many thanks!

Upvotes: 1

Views: 434

Answers (4)

Dan
Dan

Reputation: 11084

In this case, config is an array index. Namely a top level index for the $GLOBALS array.

You can use print_r($GLOBALS) or var_dump($GLOBALS) to see a nice representation of the array structure.

Upvotes: 0

Tim Lewis
Tim Lewis

Reputation: 29288

I think you need to learn the difference between an indexed array and an associative array. In Php, you can define an array two ways. The first is the simple array:

$array = array("1", "2", "3"); 

This would create an index array (index being 0 - n for length of the array) which you can access like so:

echo $array[0]; // Would echo "1"

Pretty straight forward now? The next type of array you can declare is an associative array:

$array = array("one" => "1", "two" => "2", "three" => "3");

This array is set up slightly differently. Instead of starting at 0 and adding 1 for every element in the array, you define a key => value pair for the array, and access any element using that key value:

echo $array["one"]; // Would echo "1"

Essentially, $GLOBALS is an array, and $GLOBALS["config"] is an associative entry in this array that can be accessed any time using the config key. Hope that made sense. Check the php manual for more information on arrays and their usage:

PHP Manual

Upvotes: 1

gen_Eric
gen_Eric

Reputation: 227270

$GLOBALS is an array. It's an array of all global variables in your PHP script. ['config'] is how you access elements in an array in PHP.

In the $GLOBALS array, he is setting the 'config' key to an array. That array contains 3 keys: 'init', 'remember', and 'session'.

Upvotes: 0

Robo Robok
Robo Robok

Reputation: 22703

php.net is short, but detailed enough about it: http://php.net/manual/en/reserved.variables.globals.php

Upvotes: 0

Related Questions