Hi. I haven't found a very good answer to my problem so I'm starting a new topic. I have the file english.php which has variables like $lang['fname'] = "First name";. Also I have header.php which includes english.php : include('english.php');. Now, header.php is included in another php page, let say addInfo.php. If I write in addInfo.php : echo $lang['fname']; it shows me "First name", but if i write a function in addInfo.php, as example function added () { echo $lang['fname'];} and then added(); (i tried also echo added()) it doesn't want to display the value("First name"). Does somebody know a solution for this sample(i think) problem. I'm ready to try all answers. Regards, StefanZ
+1
A:
you have to state global $lang in your function
ex.
function added(){
global $lang;
echo $lang["fname"];
}
PHP_Jedi
2010-03-09 19:55:52
+3
A:
When you write this :
function added () {
echo $lang['fname'];
}
PHP will search for a $lang
variable that is local to the function :
- it will not see the global one that's declared outside of the function.
- and, as there is no
$lang
variable set, inside the function,$lang['fname']
will benull
-- i.e. it will not display anything whenecho
ed.
To indicate to PHP that it should use the global variable from outside the function, you need to declare the variable as global
, inside the function :
function added () {
global $lang;
echo $lang['fname'];
}
For more informations, you should read the Variable scope section of the PHP manual.
Pascal MARTIN
2010-03-09 19:56:09
Yep, this can be tricky if you're used to javascript or something where external definitions are automatically allowed in the function scope.
UltimateBrent
2010-03-09 19:59:07
A:
Thanks for answers, a very sample thing makes the problems "null". Now it works.
stefanz
2010-03-09 20:50:57