1

I am trying to have a function that among other things declares global variables based on a variable that i give it. the part that fails is making the variables global

function setGlobalVariable($name) {

    global $name, $arrayname_{$name};
}

any idea? thanks :)

6
  • 3
    why would you want to pollute the global namespace with variables? Commented Nov 23, 2010 at 11:27
  • poor developer, who will support the code you've written. Commented Nov 23, 2010 at 11:27
  • Only one: Avoid global variables. Usually they are a powerful source of troubles (and you don't need them). Commented Nov 23, 2010 at 11:29
  • I agree with you all about the poor use of Global variables. What i am trying to do is write a function that does MSQL query and returns all the variables needed. Not only the array of the query but also mysql_query() & mysql_num_rows() for other uses such as a do loop to display the results. I would love to have a better way to do this but I am not an expert. Any suggestions? Commented Nov 23, 2010 at 11:36
  • Then create dependent functions that have arguments for passing results to and from other functions, instead of using the global namespace as a temporary holding area - the latter is like throwing your garbage on the street expecting that someone will pick it up later Commented Nov 23, 2010 at 11:44

4 Answers 4

6

Really, stop messing with global variables that way.

Anywaym here's your solution if you really want to do that:

function setGlobalVariable($name) {
    $GLOBALS['arrayname_' . $name] = 'yourvalue';
}
Sign up to request clarification or add additional context in comments.

Comments

3

You should not do that. Global variables are in general a sign of poor design. What is it that you are trying to achieve? I am sure that there is a better solution. Besides that, global does not work like that. global makes other variables outside your function locally available. Use $_GLOBAL to create globals.

Comments

1

Take a look at the Registry Pattern (http://martinfowler.com/eaaCatalog/registry.html).

A well-known object that other objects can use to find common objects and services.

There are various PHP implementations, for example Zend_Registry: http://framework.zend.com/manual/en/zend.registry.html

Comments

0

You're almost right, but not quite; a variable variable takes the form of ${"name"}, so what you're looking for is something like global ${"arrayname_$name"};.

http://www.reddit.com/r/programming/comments/dst56/today_i_learned_about_php_variable_variables/c12np38 is fascinating reading on the topic, if you feel so inclined.

It's likely a terrible idea, though, and if you're resorting to that sort of thing, it's a good indication that your code may be poorly designed. Consider refactoring it (for example, to keep a single known array that your other arrays are kept in, and may be referenced by key.)

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.