problem in global variable definitions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

problem in global variable definitions

Post 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
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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']);
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post 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.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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*
Post Reply