How to know if a variable has been initiated or not [modified]
-
Hello again, I'm trying to handle the language setting from the user. For that I've created a variable that I'm using in all the PHP files I have:
$MyLangVar
. I would like to assign a default value to that variable to i.e. english. So I've done this in my initialize file (a file that is being included by all the other files in the web site):<?php
include($_SERVER['DOCUMENT_ROOT'].'/important_file_for_all.php'); // that file is not using the $MyLangVar variable.// first attempt.
/*
if (!isset($MyLangVar))
{
$MyLangVar= "English";
}
*/
// second attempt
if (!isset($GLOBALS['MyLangVar'])
{
$MyLangVar = "English";
}
?>Both attempts have not worked properly IN THE 404.PHP PAGE. At the beginning of each file I have this line:
$MyLangVar = $GLOBALS['MyLangVar'];
How should I do this? Thank you in advance.[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
modified on Wednesday, March 30, 2011 2:16 PM
-
Hello again, I'm trying to handle the language setting from the user. For that I've created a variable that I'm using in all the PHP files I have:
$MyLangVar
. I would like to assign a default value to that variable to i.e. english. So I've done this in my initialize file (a file that is being included by all the other files in the web site):<?php
include($_SERVER['DOCUMENT_ROOT'].'/important_file_for_all.php'); // that file is not using the $MyLangVar variable.// first attempt.
/*
if (!isset($MyLangVar))
{
$MyLangVar= "English";
}
*/
// second attempt
if (!isset($GLOBALS['MyLangVar'])
{
$MyLangVar = "English";
}
?>Both attempts have not worked properly IN THE 404.PHP PAGE. At the beginning of each file I have this line:
$MyLangVar = $GLOBALS['MyLangVar'];
How should I do this? Thank you in advance.[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
modified on Wednesday, March 30, 2011 2:16 PM
Where is the $MyLangVar declared? if it is inside the if statement it could be lost after the if statement is completed.
-
Where is the $MyLangVar declared? if it is inside the if statement it could be lost after the if statement is completed.
Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
$MyLangVar = $GLOBALS['MyLangVar'];
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<hThen, inside the "file_that_must_be_included.php" I've done this:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');if (!isset($MyLangVar))
{
$MyLangVar = "English";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
//defining styles + favicon...Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
-
Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
$MyLangVar = $GLOBALS['MyLangVar'];
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<hThen, inside the "file_that_must_be_included.php" I've done this:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');if (!isset($MyLangVar))
{
$MyLangVar = "English";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
//defining styles + favicon...Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
If the variable is already declared it will stay global. Undefined index means that element of the array globals is not set. The most useful function in php is var_dump()! It usually tells you what you need to know. I would var_dump( $GLOBALS ) and see what you get. feel free to contact me directly if you are still having dramas.
-
Hello Nick, QUESTION PART: This is something I've not understood properly: if there is not a define, dim, type specification at the moment of declaring a variable, how one know where it has been declared? Moreover if a web is not a structured setting of pages (I mean that one can access any page of the web without any specific order thanks to the search engines). At the beginning of each file of the site I've got something like:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');
$MyLangVar = $GLOBALS['MyLangVar'];
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<hThen, inside the "file_that_must_be_included.php" I've done this:
<?php
include($_SERVER['DOCUMENT_ROOT'].'/another_global_file.php');if (!isset($MyLangVar))
{
$MyLangVar = "English";
}
?><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
//defining styles + favicon...Here it is where I'm trying to check if the variable has been initialized or not, and if it has not been initialized then I'm setting it to the default language. CONCLUSIONS / IDEAS: After writing the previous lines, I've thought: * If declaring a variable inside a "if" clause makes it local, then having that "if (!isset..." it has no sense until the gloabal variable has been declared because just after leaving the if clause the var will be "destroyed". * I guess I should change the line order in the main files: putting the "$MyLangVar = $GLOBALS['MyLangVar'];" line at the beginning, just before the "include($_SERVER['DOCUMENT_ROOT'].'/file_that_must_be_included.php');". In that way I've declared the $MyLangVar, it will become global for all the file (I guess, please confirm that) and therefore I'll only initialize it once (inside the sub_file "/file_that_must_be_included.php". * I've tried that but in the 404.php file I can see a message telling me: Undefined index MyLangVar'... Lost again... :sigh: Can you do something with all this data? Thank you in advance! :thumbsup:
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
come to think of it $GLOBALS will not be storing the values once the pages when another page is loaded. What you want is
-
come to think of it $GLOBALS will not be storing the values once the pages when another page is loaded. What you want is
You are my idol for today! :thumbsup: Thank you very much for your help! $_SESSION has made it! :-D
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
-
You are my idol for today! :thumbsup: Thank you very much for your help! $_SESSION has made it! :-D
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.
-
Now I'm using EasyPHP 5.3.5.0.
[www.tamelectromecanica.com] Robots, CNC and PLC machines for grinding and polishing.