0

I'm using Netbeans IDE, it shows a warning next to undeclared variables - very useful.

If I have this at the top of a file

global $CFG;

then the warnings go away because the variable has been declared.

But is this good practice? Are they any advantages? The code still works without the declaration.

Note: This is for files that have code outside of functions.

Russell England
  • 7,837
  • 1
  • 21
  • 37
  • 5
    The good practice is not to use globals at all - they are pure evil. – zerkms Mar 10 '13 at 22:58
  • This is a bad code smell. The odds are there are big improvements needed in your code. – John Conde Mar 10 '13 at 22:58
  • 1
    I think people may be misunderstanding the OP's intentions...at face value it seems he's simply using a variable within the global scope and php is giving him a notice because it hasn't first been defined – Crayon Violent Mar 10 '13 at 23:06
  • Thanks chaps, I'm working with an open source system used world wide that shall rename nameless :) It just happens to have some key global variables. I prefer to declare all variables that are being used in the following code, but it doesn't seem to be common practice with this particular system. My question is about the practice of declaring global variables rather than should they be used at all. Cheers. – Russell England Mar 10 '13 at 23:22

3 Answers3

1

it is good practice to declare variables before using them. Declaring them as global within the global scope is superfluous though. You could just do

Instead of doing

global $CFG;

you can just do

$CFG;

The only time declaring them with the global prefix is "useful" is when you do it inside a function to access a globally scoped variable from within the function - but this is usually bad practice, very few cases where this is absolutely necessary.

Crayon Violent
  • 30,524
  • 3
  • 51
  • 74
0

No, if you wish to use a variable outside of its scope (for example inside a function) you may pass it or globalize it inside the function

function xoxo(){
   global $var;
}
Iesus Sonesson
  • 854
  • 6
  • 12
0

There are two things which would make me wary of "declaring" variables in this way.

Firstly, large amounts of code outside of any function probably mean that your code needs re-factoring. At the top level of your code, you might have a handful of lines which call the main actions of the script or page, but saying your declarations would go "at the top of the file" suggests there is rather more than this.

Secondly, "declaring" a variable in PHP is usually synonymous with giving it an initial value of some kind. For instance, setting $params = array() before building up a list of template parameters such as $params['foo'] = get_foo(). Such initialisation should always be kept close to the code using it, so that if you do later re-factor it, the code goes with it.

An example of why always initialising a variable is a good idea is if you end up putting an entire block of code inside a loop of some sort. In the case above, if I was rendering multiple templates and forgot to initialise $params, $params['foo'] could end up being passed to every single template.

IMSoP
  • 65,743
  • 7
  • 83
  • 127