Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/235847-variable-variable-array-call-how/
Share on other sites

$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?

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.

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.

 

 

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?

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.