0

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

5
  • Can I use them outside of the function ? if so how ? Commented Jun 28, 2016 at 14:12
  • What are you trying to do? I'm not following the goal. Commented Jun 28, 2016 at 14:38
  • 1
    you are missing an as between in you foreach Commented Jun 28, 2016 at 14:38
  • The as is in my code, I missed it when I typed it in on here.. Commented Jun 28, 2016 at 15:14
  • The goal is to have the variables created in the function available to the rest of the script. Commented Jun 28, 2016 at 15:15

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

I had thought about that - but as it's dynamic I won't know what values are in the array or what order they are in.
Give every option a key and a value and when you save them in an array you save them like $options[$option_name] = $option_value;
How do I make the array available to the rest of the page ?
OK I've sorted it... and it looks like this will work for me :) thanks
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.