Skip to content
Advertisement

How to declare a global variable in php?

I have code something like this:

JavaScript

is there any way to define the global variable in one place and make the variable $a accessible in all the functions at once? without making use of global $a; more?

Advertisement

Answer

The $GLOBALS array can be used instead:

JavaScript

From the Manual:

An associative array containing references to all variables which are currently defined in the global scope of the script. The variable names are the keys of the array.


If you have a set of functions that need some common variables, a class with properties may be a good choice instead of a global:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement