Jump to content

global variables


anatak

Recommended Posts

I have a question about global variables.
I would like to create some vars that are available throught the whole program
for 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 you
anatak
Link to comment
https://forums.phpfreaks.com/topic/26995-global-variables/
Share on other sites

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

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 ?
EDIT
also 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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.