[pmwiki-users] php globals q.

Hans design5 at softflow.co.uk
Thu Oct 2 09:12:42 CDT 2008

Thursday, October 2, 2008, 1:50:23 PM, noskule wrote:

> I would like to use variables in a function. Normaly I achive this by 
> declaring varX as global.

> function() {
>     global $X $Y $Z

> The problem is know that I use a set of variables in a couple of 
> functions an I would like to avoid adding/changing these global 
> variables in each function. So my question. Is it possible to declare a
> set of variables without writing every variable name, instead use an 
> array or function to declare them, something like:

> function() {
>     global $myvarsetarray

> function() {
>     myvarset(X)

If you define your variables in the script, like a recipe script
which is included in config.php (but NOT a skin.php script, which
actually runs in a function), then you can use these variables in any
function, by just declaring them global in each function you wish to
use them.

If you define a variable in a function, and declare it global, it can
also be available in another function, if it is declared global there
too, and this function is run later than the first function. No need
to redefine the variable.

Instead of using lots of variables, you could define an array.
$MyVars = array('value1','value2','value3');
and then access each through $MyVars[0], $MyVars[1], $MyVars[2]
$MyVars = array(
  'var1' => 'value1',
  'var2' => 'value2',
  'var3' => 'value3'
and access them like $MyVars['var1'] , $MyVars['var2'] etc.

Then in a function you just add
    global $MyVars;
to have the array of variables available.


More information about the pmwiki-users mailing list