Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The manual on "extract" shows you can extract an array like:

extract(array('one'=>1,'two'=>2));

into $one,$two...

But the extract function doesn't return the variables. Is there a way to 'globalize' these variables? Maybe not using extract, but a foreach loop?

EDIT: (explanation about what I'm trying to achieve) I have an array containing hundreds of output messages which I want to have accessible as variables efficiently. What I mean is that whenever I want to output a message, say:

$englishMessages = array('helloWorld'=>'Hello World');
$spanishMessages = array('helloWorld'=>'Hola Mundo');
'<span id="some">'. $helloWorld .'</span>';

The message would appear. The reason I'm doing it like this is so that users can change the language they're viewing the website in, so something like: ''. $helloWorld .''; would produce:

Hola Mundo!
share|improve this question
1  
Global variables and extract() are two of my least favorite parts of PHP... so, this question made me throw up a little in my mouth. Can solve your application problem another way? – ojrac Feb 7 '10 at 16:42
@ojrac: absolutely, I edit with a more elaborate explanation. – Gal Feb 7 '10 at 16:44
1  
I'm rusty on the syntax, but here's an idea: $language = get_lang('en'), and then "<span>${language['helloWorld']}</span>" – ojrac Feb 7 '10 at 16:51
@ojrac, the reason I didn't use this to begin with is because I want to use variables, not an array. – Gal Feb 7 '10 at 16:56

6 Answers

up vote 4 down vote accepted

Not exactly an answer to your question ...but: Keep the array, don't pollute the (global) variable namespace.

$englishMessages = array('helloWorld'=>'Hello World');
$spanishMessages = array('helloWorld'=>'Hola Mundo');

// wrap this in a nice function/method
$lang = $englishMessages;
// then use $lang for the output
'<span id="some">'. $lang['helloWorld'] .'</span>';

Some variations on the same theme:

function getMessages($language) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  );
  // <-- add handling reporting here -->
  return $l[$language];
}

$lang = getMessages('en');
echo '<span id="some">'. $lang['helloWorld'] .'</span>';

or

function __($language, $id) {
  static $l = array(
    'en'=> array('helloWorld'=>'Hello World'),
    'es' => array('helloWorld'=>'Hola Mundo')
  );
  // <-- add error handling here -->
  return $l[$language][$id];
}

echo '<span id="some">'. __('es', 'helloWorld') .'</span>';

You might also be interested in http://docs.php.net/gettext

share|improve this answer
do you suggest not making $lang global? – Gal Feb 7 '10 at 17:03
Neither $lang itself nor its elements, yes. Just use a function/method that returns an array. – VolkerK Feb 7 '10 at 17:06
+1 for a creative use of local static variables! – SteveK May 15 '12 at 2:43
 $GLOBALS += $vars;

for example

function foo() {
  $vars = array('aa' => 11, 'bb' => 22);
  $GLOBALS += $vars;
}

foo();
echo $aa; // prints 11

that said, can you explain why you need this? Using global variables is considered poor style, maybe there's a better way

share|improve this answer
I have an array containing hundreds of variables=>output messages. Do you think there's a better practice for this? (I don't want these messages to be constants because they might have to be changed). – Gal Feb 7 '10 at 16:41

using extract() on an array will create new variables inside the current scope, or assign new values to existing variables.

I'm not sure what you mean by "globalize", but my understanding is that you are trying to accomplish exactly what extract() does, just be mindful of the current scope (if you extract() from within a function, the extracted variables will only be available inside the function itself).

share|improve this answer
for reference, in case im ever searching again for this ... extract works with assoc-array, use list for indexed arrays – farinspace Aug 4 '11 at 19:58

You mean:

 foreach($array as $var_name => $var_value)
 {
       global $$var_name;
       $$var_name = $var_value;
 }

This will global each variable, and then set it to your value. For yours it would create $one, and $two

share|improve this answer
foreach ($array as $key => $value) {
  // initialize the variables as global
   global $$key;
   $$key = $value;
}

EDIT:

Just noticed a mistake of mine, you'd have to make the key of the array into a variable, which can be done by converting it to a variable variables using $$ notation.

share|improve this answer
This doesn't work, as $key will only be the value of the last item in the array. – Chacha102 Feb 7 '10 at 16:41
I had edited my answer to correct a few things – Anthony Forloney Feb 7 '10 at 16:42

Are you looking for the variables that have been extracted? You can find them using array_keys()

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.