mrbean Posted May 8, 2011 Share Posted May 8, 2011 hi all, I have an language pack for example: languages/en.php: $en['mail']['letter closing'] = "regards,\n your friend!"; and in my config: $language = "en"; $include_language = @include("languages/".$language.".php"); if(!($include_language)) { $try_default_language = @include("languages/nl.php"); if(!($try_default_language)) { echo "kan de taalpakket niet vinden<br>"; echo "Could not find the language pack.<br>"; echo "example on error: ".$test." shows nothing"; exit; } } In my function I want to include the language pack for example i have $language = 'en' so I want to include $en['general']['letter closing'] I will do this: global $language,${$language}['general']; But that gives an error unexpected '[' blah blah. How can i call the variable variable array in the valid php way? Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/ Share on other sites More sharing options...
denno020 Posted May 8, 2011 Share Posted May 8, 2011 Are you supposed to be using curly braces instead of square braces?? Denno Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212343 Share on other sites More sharing options...
mrbean Posted May 8, 2011 Author Share Posted May 8, 2011 $language = "en"; if(${$language} == $en) TRUE so becomes $en['general']['letter closing'] but it will give an error if i do global ${$language}['general']['letter closing']; // this will not work same as: global $en['general']['letter closing']; // this will work do you understand what i am trying to do? so my question is: How can i call the variable variable array in the valid php way? Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212346 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2011 Share Posted May 8, 2011 You are making this harder than needed. Each language file should use the same array name so that after you include the correct language file, you don't need to do anything else in your code. In the en.php file you would have - $lang['mail']['letter closing'] = "regards,\n your friend!"; In the spanish.php file you would have - $lang['mail']['letter closing'] = "lo que se refiere,\n su amigo!" You would always reference $lang['mail']['letter closing'] in your code to access the correct phrase for the language file that was included. Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212351 Share on other sites More sharing options...
mrbean Posted May 8, 2011 Author Share Posted May 8, 2011 I do making this harder than needed, but I want to learn that so in other cases when I'ts not possible to make it easier it will be usefull. I will do it the way u said but I don't see this as solved so I will keep this open for other suggestions. Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212356 Share on other sites More sharing options...
PFMaBiSmAd Posted May 8, 2011 Share Posted May 8, 2011 Variable variables take three times longer to execute, so they are a poor choice when there are other ways of accomplishing the same task. From the php.net documentation for what you are trying to do - In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second. Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212359 Share on other sites More sharing options...
mrbean Posted May 8, 2011 Author Share Posted May 8, 2011 In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a[1] then the parser needs to know if you meant to use $a[1] as a variable, or if you wanted $$a as the variable and then the [1] index from that variable. The syntax for resolving this ambiguity is: ${$a[1]} for the first case and ${$a}[1] for the second. second case example: ${$language}['succesfull login']; So i did it on the correct way but the php parser will give an error in other words an bug? Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212361 Share on other sites More sharing options...
wildteen88 Posted May 8, 2011 Share Posted May 8, 2011 Probably because you're trying to access an item from your array when defining ${$language}['succesfull login'] as global. You cannot defined an array item as global only variables can be defined as global. You should ideally pass your variables to your function rather than define them as global. Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212376 Share on other sites More sharing options...
mrbean Posted May 8, 2011 Author Share Posted May 8, 2011 Thats right i cant sorry So its solved by PFMaBiSmAd thanks Quote Link to comment https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/#findComment-1212388 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.