具有已知参数的数组

I've not that new to php but am trying to figure out how exactly this piece of code reads.

$actions = array();  

function add_action( $hook, $function )  
{  
    global $actions;  

    // create an array of function handlers if it doesn't already exist  
    if( !isset( $actions[ $hook ] ) )  
        $actions[ $hook ] = array();  

    // append the current function to the list of function handlers  
    $actions[ $hook ][] = $function;  
}  

here is my understanding but I believe I'm completely wrong - variable actions is given an array with no params, then a function is created and actions is made into a global giving it global scope. Then there a condition the bit I don't understand that somehow checks for a parem. $actions[ $hook ] its what this bit means that confuses me. is it calling into the array? and why are blank arrays set to variables?

Thanks for clearing this up for me

First we are declaring a php variable $actions within the global scope:

$actions = array();

Then in the function, you use the global keyword to declare that you are using an already defined global value instead of creating a new value within the scope of the function:

global $actions;

Next, it is using the isset() function to check if the value $hook exists as an index of our global $actions. If the index does not exist, it creates that index and assigns it an empty array as the value.

// create an array of function handlers if it doesn't already exist  
if( !isset( $actions[ $hook ] ) )  
    $actions[ $hook ] = array();

Last, it is using the bracket operator on the array it was just given to append a new value to it, in this case it is giving it the value of $function.

// append the current function to the list of function handlers  
$actions[ $hook ][] = $function;  

So in the end, you have the global $actions variable with an index of $hook which has an array value, and that array value has an index added to it with the value of $function.

global $actions doesn't make the variable a global, it was already a global simply because it was created outside any function. This declaration allows the function to access the variable; normally, a function can only access variables that are created within the function.

$actions is a 2-dimensional array. The first dimension is an associative array, keyed off hook names. The second dimension is a linear array of functions that are associated with that hook. The likely use of this is that when a hook is triggered, all the associated functions will be run.

if (!isset($action[$hook])) checks whether there's already an entry in the $actions array with the key $hook. If there isn't, a new entry is created containing an empty array.

$actions[$hook][] = $function then adds an element to the array of actions for that hook.

To see how this works, I suggest you run a simple script that calls add_action() repeatedly, and calls print_r($actions) occasionally:

print_r($actions);
add_action('hook1', 'func1');
print_r($actions);
add_action('hook2', 'func2');
print_r($actions);
add_action('hook1', 'func3');
add_action('hook3', 'func4');
add_action('hook2', 'func5');
print_r($actions);

Blow-By-Blow Walkthrough

The code you posted creates an empty array called $actions. Inside of the function it then uses the global statement to give it access to that array because it was declared outside of the function. This is necessary because otherwise the function would reset the array on every run if it was declared inside it.

The conditional (if) checks to see if the variable $hook is a key of the array and if not, creates an array element the name of which is the value of $hook and initializes it to an empty array.

It then adds the value of the variable $function as an element of the array that either already exists or was just created.

Analysis

This code is designed to create a list of functions that are called at specific "hook" points in code at which those functions are called, here is an example implementation:

add_action('start', 'foo');
add_action('start', 'bar');
add_action('end', 'baz');

Then at various points in code execution, there are designated points at which it checks for actions to run, for example at the very beginning of the code:

foreach($actions['start'] as $function) {
    $function();
}

The above would call foo() and bar(). Then at the end of the code you can repeat the process to call any hooks there (baz).

foreach($actions['end'] as $function) {
    $function();
}

The above would call baz().