0

I've this php file (textMessages.php) where I'd like to define some php arrays ...

<?php
  /*
  -----------------
  Language: Italian
  -----------------
  */
  $langIT = array();
  $langIT['LANG__CHOICE'] = 'Seleziona la lingua';
 /*
  -----------------
  Language: English
  -----------------
  */
  $langEN = array();
  $langEN['LANG__CHOICE'] = 'Select the language';
?>

In a second php file (myFile.php) I'd like to use the two php array above for example (this code DOES NOT work ...)

<?php
 include("textMessages.php");
 ....
 ....
 ....
     function myFunction($var1, $var2, ...)
    {
     ....
     $langCurrent = array();
     $langCurrent = $langEN;
     .....
    }
?>

when I try to execute I obtain this error ...

PHP Notice:  Undefined variable: langEN

How may I to share the array defined in the first file to using them in the second one?

Cesare
  • 1,629
  • 9
  • 30
  • 72
  • try creating classes and passing the $landEN as a parameter to a function or constructor. – MalcolmInTheCenter Mar 30 '17 at 20:47
  • 1
    When you say it doesn't work, are you getting an error in the log? If you `var_dump($langCurrent);` at the end of myFile.php what does it return? – Matt Mar 30 '17 at 20:47
  • I've update my question with the error ... anyway is .... PHP Notice: Undefined variable: langEN – Cesare Mar 30 '17 at 20:53
  • Change your include to require and see if it crashes because it can't find the file. – Matt Mar 30 '17 at 20:54
  • Can you restructure to `$langCurrent = "EN"; echo $lang[$langCurrent];`. Where `$lang["EN"] = "Select the language` and `$lang["IT"] = "Seleziona la lingua"`? – JustCarty Mar 30 '17 at 20:58
  • This is only a sample .. I've to manage long strings arrays ... – Cesare Mar 30 '17 at 21:00
  • 1
    Your code should work. Is the `$langCurrent = $langEN;` line inside a function? – Barmar Mar 30 '17 at 21:05
  • @Carty If it's a translation file, he needs more than one phrase for each language. – Barmar Mar 30 '17 at 21:06
  • 1
    The proper way to do this is with a 2-dimensional array: `$lang['EN']['LANG__CHOICE'] = 'Select the language'; $lang['IT']['LANG__CHOICE'] = 'Selzione la lingua';` – Barmar Mar 30 '17 at 21:07
  • @Barmar ... yes my code in MyFile.php is inside a function ... – Cesare Mar 30 '17 at 21:09
  • @Barmar That's what I was going for ;) PS writing code on my phone is not the best D: – JustCarty Mar 30 '17 at 21:09
  • 1
    @Carty Then you need to use `global $langEN;` in the function. – Barmar Mar 30 '17 at 21:12
  • @Barmar I've updated the code in my original question ... now is clearer ... – Cesare Mar 30 '17 at 21:15
  • 1
    @Barmar Or pass the language as a parameter? I'm not a fan of "global" – JustCarty Mar 30 '17 at 21:15
  • 1
    @Carty Indeed. The duplicate question I linked to describes many different ways to solve the problem, including passing the array as a parameter. – Barmar Mar 30 '17 at 21:17
  • using global now is working .. I'll try to modificate my code to avoid to use "global" – Cesare Mar 30 '17 at 21:20
  • @Cesare just pass it as a parameter in the function that it's contained in. That should be neater and avoids the global variable. – JustCarty Mar 30 '17 at 21:33

1 Answers1

1

Your code is correct. To double check I copied it to my local environment. What error do you get? Did you try to print_r($langEN)?

Another common approach is to return the array in "textMessages.php" and then use $langCurrent = include("textMessages.php");

However you should use one file per language then.

Sebastian
  • 416
  • 1
  • 6
  • 19
  • Thanks! .... I've update my question with the error ... anyway is .... PHP Notice: Undefined variable: langEN – Cesare Mar 30 '17 at 20:54
  • If you include the file, the variable is in the global namespace (bad practice). You have to either pass $langEN into the function when calling it, or make it visible inside the function with "global $langEN" Take a look at this: [http://php.net/manual/en/language.variables.scope.php] – Sebastian Apr 02 '17 at 08:56