Tommy
Tommy

Reputation:

Cannot understand this php parser error

Why does this give me parse error? going crazy over here..

I have this file right:

function start_connection() {

 global $config['db_host'];

}

Parse error: parse error, expecting ','' or';'' in functions.php on line 5

Upvotes: 1

Views: 141

Answers (3)

TheHippo
TheHippo

Reputation: 63139

just do:

function start_connection() {
 global $config;
 echo $config['db_host'];
}

You can only use global variables, but not a specific value of a array.

Upvotes: 1

Nathan
Nathan

Reputation: 1700

when declaring the global variable do not include the array key, so:

global $config;

this will provide access to all key => value pairs within $config array within the function start_connection().

Upvotes: 8

Tyler Carter
Tyler Carter

Reputation: 61557

You can't global an array within a variable. Only the variable.

Upvotes: 1

Related Questions