2
function first() {
    foreach($list as $item ) {
        ${'variable_' . $item->ID} = $item->title;
        // gives $varible_10 = 'some text'; (10 can be replaced with any number)
    }
    $ordinary_variable = 'something';
}

How to get values of this function inside an another function?

Like:

function second() {
    foreach($list as $item ) {
        get ${'variable_' . $item->ID};
        // getting identical value from first() function
    }
    get $ordinary_variable;
}
  • We know that $variable_id (id is numeric) already exists in first()
  • $list is an Array(), which can have more than 100 values.
  • $ordinary_variable is a string.

Thanks.

James
  • 42,081
  • 53
  • 136
  • 161

2 Answers2

6

You could let the first function return an array:

function first() {
    $values = array();
    foreach($list as $item ) {
        $values['variable_' . $item->ID] = $item->title;
        // gives $varible_10 = 'some text'; (10 can be replaced with any number)
    }
    $values['ordinary_variable'] = 'something';
    return $values;
}

and then:

function second() {
    $values = first();
    foreach($list as $item ) {
        $values['variable_' . $item->ID];
        // getting identical value from first() function
    }
    $values['ordinary_variable'];
}

or pass it as parameter:

second(first());

I would advice against global as this introduces side-effects and makes the code harder to maintain/debug.

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
0

${'variable_' . $item->ID} is out of scope. Perhaps you should create a global array and store them there.

simplified example

$myvars = array();

function first() {
  global $myvars;
  ...
  $myvars['variable_' . $item->ID] = $item->title;
}

function second() {
  global $myvars;
  ...
  echo $myvars['variable_' . $item->ID];
}
Fosco
  • 38,138
  • 7
  • 87
  • 101
  • Global is bad and unnecessary. Argument lists exist for a reason. – Major Productions Aug 05 '10 at 14:37
  • 1
    It may be unnecessary, but it's not necessarily bad. No context was provided for the interplay between the two functions. – Fosco Aug 05 '10 at 14:40
  • Global obfuscates the interaction between what a function does and what it needs to actually do its job. It leads to debugging, maintenance, and extensibility issues. A function's interface needs to be truthful. – Major Productions Aug 05 '10 at 14:44
  • Oh please. Just because you've never encountered that situation (yet) doesn't mean that ignoring best practices is a good idea. They're considered best practices for a reason. Yes, _sometimes_ best practices are an idealized goal that shouldn't get in the way of development. 'Global' is not one of those as it doesn't offer anything that passing a variable through an argument list cannot also do. Your code example, for instance, would work just as well by passing the array in by reference. Further, doing that would make it clear to whomever invokes the function that the array is necessary – Major Productions Aug 05 '10 at 15:03
  • Last comment: goto-like structures are also included in most languages, even modern ones. I don't think anyone would say using them is good, or that if one is tempted to use one it's likely a sign of bad design. The only global-like structure I've ever used is a Singleton. And only after careful consideration and care in its use. – Major Productions Aug 05 '10 at 15:51
  • My 2 cents: Globals are okay to use in a case like this. I wouldn't recommend them as a general tool, though, so I have to agree with @kevinmajor's basic point. Proper OOP is that much better :) I agree though that not using globals often results in using singletons or static classes (which is in no way better in regards to testability and keeping a clean structure) and not using globals *properly* is difficult. I asked a related question a while back that brought a lot of feedback: http://stackoverflow.com/questions/1812472/in-a-php-project-how-do-you-organize-and-access-your-helper-objects – Pekka Aug 05 '10 at 17:13