Page 1 of 1

problem in global variable definitions

Posted: Thu Apr 08, 2004 4:18 pm
by davidklonski
define a global array called translation in a file called english.php:

Code: Select all

<?php
$translation = Array();
$translation ['hi'] = "hi";
$translation ['bye'] = "bye";
?>
a second file called LanguageManager.php has the following definition:

Code: Select all

<?php
$languageData = Array();
$languageData['English'] = Array();
$languageData['English']['file'] = "english.php";
?>
This is the file that uses the code:

Code: Select all

<?php
include_once(LanguageManager.php);

global $languageData;
include($languageData[$language]['file']);

gloabl $translation ;

print $translation ['hi'];
?>
I use the last segment of code whenever I want to display text to the screen.
The problem that sometimes I get a warning saying that 'English' was never defined as an index to $languageData.

What could cause this?

thanks

Posted: Thu Apr 08, 2004 4:25 pm
by markl999
You don't need those two 'global' lines.
You say this only happens 'sometimes' .. which suggests it's $language that's at fault.. where is $language being set? My money's on a newline char being on the end of it, so maybe all you need to do is:
include($languageData[trim($language)]['file']);

Posted: Thu Apr 08, 2004 4:29 pm
by davidklonski
no, assume that $language is fine and set to 'English'

When I said that sometimes it happens and sometimes it doesn't I meant that I use this code in two files.

In the first one everything works out fine but in the second one I get the warning.

Posted: Thu Apr 08, 2004 4:33 pm
by markl999
Well, as that code stands, it's fine. So there must be something this second file does that the first doesn't (or vica versa).
If i copy your first 2 files and use this code to test, then it works 100% of the time

Code: Select all

<?php
include_once('LanguageManager.php');

$language='English';
include($languageData[$language]['file']);

print $translation ['hi'];
?>
The problem has to be in something we're not seeing *shrug*