2

Why does the following result in an error message? (it is declared inside a function in PHP by the way; $a and $b are alphabetic values, not numeric)

global $pre[''.$a.''], $predis[''.$b.'']; Parse error: syntax error, unexpected '[', expecting ',' or ';' in ... 
3
  • 3
    Just FYI, don't do $pre[''.$a.''], just do $pre[$a]. Commented Jun 21, 2011 at 2:06
  • @Rocket even if it is alphabetical and not numeric (ex: "abc" and not "123")? Commented Jun 21, 2011 at 2:08
  • 4
    Yes that is correct you don't need the '' even with a non-numeric key. Commented Jun 21, 2011 at 2:13

3 Answers 3

6
global $pre, $predis; 

Then use $pre and $predis how you want.

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

Comments

6

I may be mistaken, but I believe you cannot selectively use keys as globals. You would have to do

global $pre, $predis; 

and then use the keys.

The problem lies in that while the variables $pre and $predis have unique identifiers, the keys are only identified in reference to their variables, such that $var1['key'] != $var2['key']. There is no way to assign that specific key to an identifier while making it global, at least in one step. You could however, use an intermediate variable, like

$prekey = $pre['key']; global $prekey; 

Comments

6

You can't make array index global. In this case, only $pre and $predis may be global and you can use their contents as you need:

global $pre, $predis; 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.