Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create local variable in calling function, from a string

Tags:

php

I want to be able to call a function, that will set one or more local variables in the calling function. For instance:

function someFunc () {
 loadTranslatedStrings($LOCALS, "spanish");
 echo $hello; // prints "hola";
 }

function loadTranslatedStrings (&$callerLocals, $lang) {
 if ($lang == 'spanish')
   $callerLocals['hello'] = 'hola';
 else if ($lang == 'french')
   $callerLocals['hello'] = 'bonjour';
 else
   $callerLocals['hello'] = 'hello';
 }

(I'm guessing it is impossible to do this, but might as well ask...)

like image 232
rob Avatar asked Jun 21 '26 11:06

rob


1 Answers

You could do this...

function someFunc () {
   loadTranslatedStrings($lang, "spanish");
   extract($lang); 
   echo $hello; // prints "hola";
}

CodePad.

like image 112
alex Avatar answered Jun 23 '26 00:06

alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!