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.