# Function to load vars in PHP?



## aximbigfan (Mar 8, 2008)

I made an app in PHP, where a large string of vars is loaded (and can be reloaded in certain instances to validate them).

Basically, I want to have a function where vars are loaded, but I don't want them to be global, because they need to stay in the validation functions.

So, I know this won't work, but this is what I want to do:

INSTEAD of this:

```
$var1 = $systemvars['var1']
$var2 = $systemvars['var2']
ect.....
```

I WANT to do this:

```
function load_vars()
{
$var1 = $systemvars['var1']
$var2 = $systemvars['var2']
ect.....
}

function valid()
{
load_vars);
if (isset($var1 ))
echo "hi!";
}
```

Thanks.
chris


----------



## Disparia (Mar 8, 2008)

Why the separate vars? Could pass the $systemvars array to the functions that need it.

Because of a function's scope, the array passed (unless passed by reference or globalized) will not be changed outside of the function.


```
$systemvars['var1'] = 'something';
$systemvars['var2'] = 'isgoingon';

function valid($sysarray) {
	if (isset($sysarray['var1']))
		echo "hi!";
}

//Call
valid($systemvars);
```

May or may not be what you need depending on what you're trying to accomplish. (I'm assuming there's more to that function).


----------



## Disparia (Mar 8, 2008)

This is probably the way you were thinking it:


```
function SystemVars() {
	$sys['var1'] = 'foo';
	$sys['var2'] = 'bar';
	$sys['var2'] = 'moo';
	return $sys;
}

function valid() {
	$vars = SystemVars();
	if (isset($vars['var1'])) {
		echo 'Hi';
	}
}
```

Not that it's going to matter a whole bunch, but that's a function call and array init every time another function calls it. My first example is faster, but we're only talking thousandths of a second so no big diff.


----------



## aximbigfan (Mar 8, 2008)

Never mind. I figured it out.


```
$var = "\$var = \"Hey!!!!!!!\"";
eval($var);
```

Chris


----------

