Tom
Tom

Reputation: 1618

PHP use dynamic variables from function in script?

I have a function that reads a config file and then makes variables from the values in the file.

$arr is an array of values read from a text file and then this is turned into variables.

foreach ($arr as $k => $v) {
  ${$k} = trim($v);
}

This is all done within a function. However I'd like to use the variables created elsewhere in the page. As they are dynamically created I don't know what they will all be called.

Is there any way to do this ?

Thanks

Upvotes: 0

Views: 42

Answers (2)

Buck
Buck

Reputation: 187

EDIT : Push them all in an array in your function and get the array elsewhere.

Upvotes: 0

b0ne
b0ne

Reputation: 655

You could create an array where you would keep all your options and make it global in your function and then all the options will accessible inside and outside of the function. Check this out.

Upvotes: 1

Related Questions