0

I'd like to require files in my index.php file and use the array set in those files. The tree structure from where to get the files is:

root/lang/nl/dashboard/default.php

root/lang/nl/dashboard/users.php

The code for those files is

default.php

// Default DASHBOARD language list


    if (empty($lang) || !is_array($lang)){
        $lang = [];
    }
 $lang = array_merge($lang, [

 'DASH_LANG_DE'             => 'Duits',
 'DASH_LANG_FR'             => 'Frans',
 'DASH_LANG_GB'             => 'Engels',
 'DASH_LANG_NL'             => 'Nederlands',

 'DASH_SET_LANG'            => 'Stel hier uw taal in'


 ]);

users.php

// Default USERS language list


        if (empty($lang) || !is_array($lang)){
            $lang = [];
        }


 $lang = array_merge($lang, [

     'USERS_DEFAULT'            => 'Gebruikers',
     'USERS_CREATE_NEW'         => 'Maak een nieuwe gebruiker aan',
     'USERS_DELETE'             => 'Delete de gebruiker',

     'USERS_NAME'               => 'Naam'


 ]);

index.php is placed in the root with the following code

    define ('LANG','lang/');

    function lang_files($language,$path,$file){

      foreach ($file as $value) {
                if(file_exists(''.LANG.$language.'/'. $path .'/'. $value .'.php')){
                    require_once ''.LANG.$language.'/'. $path .'/'. $value .'.php' ;
                }
                else{
                    echo ''.LANG.$language.'/'. $path .'/'. $value .'.php <b>does not exist!!</b>' ;echo "<br>";
                }
        }  
    }

    $language = "nl";

    $path = 'dashboard';

    $file =['default',
                'users'
                ];

    lang_files($language,$path,$file);

    echo $lang['DASH_SET_LANG'];

When I get this to work I will place the function as a method in my class file but first it has to work.

At this time I can not acces the array with

echo $lang['DASH_SET_LANG'];

When I require the files directly I have no issues.

When I echo out the require_once part in my function I can see that the path to my file is correct.

Does anybody have a clue where I'm going wrong?

Update

With the help I got from @vvondra I was able to figure out where I went wrong.

Below you will see the changed function. Minor change tho.

function lang_files($language,$path,$file){
        $lang = [];
          foreach ($file as $value) {
                if(file_exists(''.LANG.$language.'/'. $path .'/'. $value .'.php')){
                    require_once ''.LANG.$language.'/'. $path .'/'. $value .'.php' ;
                }
                else{
                    echo ''.LANG.$language.'/'. $path .'/'. $value .'.php <b>does not exist!!</b>' ;echo "<br>";
                }
            }
        return $lang;
    }

Thank you @vvondra for putting me in the right direction.

2 Answers2

0

functions in php create scope. this means any variable defined inside a function is not visible outside of it's body

function foo()
{
    $bar = 1;
}

// Here $bar is undefined
foo();
// Still undefined

In you case, you initialize the $lang variable inside the lang_files function, so it's not visible outside. You must return it as a return value of the function and assign it to another variable.

vvondra
  • 3,022
  • 1
  • 21
  • 34
0

This question/answer from StackOverflow might help you do things easier and more manageable.

How to load return array from a PHP file?

Basically, you only need to return the array in the files, and then in the index you include the file you want to load, assigning the include to a variable.

It works like a charm :)

Community
  • 1
  • 1
xarlymg89
  • 2,552
  • 2
  • 27
  • 41
  • I did try it according the topic you mentioned but it only returned the array in the first file. It should merge the arrays from all files before returning. – Dennis Kuijpers Jun 20 '15 at 18:37
  • Then keep gathering all arrays and combine them with the array_merge function. You have the basics, now you need to do something with them. But that is how I'd do it, gather the results of the includes merging them with your results array. – xarlymg89 Jun 21 '15 at 21:47