anatak Posted November 12, 2006 Share Posted November 12, 2006 I have a question about global variables.I would like to create some vars that are available throught the whole programfor example domain name (if you ever change domain name you don't have to go search throught your code)another one would be default language for a multilanguage website.What would be the best way to do this ?I know about the $_GLOBALS array but I thought that $_GLOBALS are unsafe to use for some reason.can anyone explain how I would be able to use a global variable ?thank youanatak Link to comment https://forums.phpfreaks.com/topic/26995-global-variables/ Share on other sites More sharing options...
trq Posted November 12, 2006 Share Posted November 12, 2006 You really should try and avoid globals where possible, you really shouldn't need them. Just define what you need as constants and include the file they are defined in when you need them. Link to comment https://forums.phpfreaks.com/topic/26995-global-variables/#findComment-123448 Share on other sites More sharing options...
heckenschutze Posted November 12, 2006 Share Posted November 12, 2006 I totally agree with thorpe's reasoning, however this is how you use them:[code]<?php$myvar = "hello";MyFunc();function MyFunc(){ global $myvar; $myvar = "testing"; // my var is now 'testing'}?>[/code]In my opinion how PHP handles scopes premotes bad coding practice, this allows you to have the same variable name in the global scope as in a function for example, but 2 totally different variables. Link to comment https://forums.phpfreaks.com/topic/26995-global-variables/#findComment-123449 Share on other sites More sharing options...
anatak Posted November 12, 2006 Author Share Posted November 12, 2006 the problem with declaring them in a include file is that you have to include that file in every function that wants to read the values of the variables.Would a public class containing the variable be a reasonable work around the insecurity global variable pose ?EDITalso if I include the file in every function am I not creating everytime new variables ?anatak Link to comment https://forums.phpfreaks.com/topic/26995-global-variables/#findComment-123455 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.