38

I'm trying to figure out how I can use a variable that has been set outside a function, inside a function. Is there any way of doing this? I've tried to set the variable to "global" but it doesn't seems to work out as expected.

A simple example of my code

$var = '1';

function() {
    $var + 1;
    return $var;
}

I want this to return the value of 2.

RST
  • 3,899
  • 2
  • 20
  • 33
Henrikz
  • 431
  • 2
  • 5
  • 7
  • 2
    From `i want this, to return the value of 2.` I would recommend doing `$var = function($var) { return $var + 1; }`. That way, you avoid using global variables, which are a bad design schema. – Francisco Presencia Feb 12 '14 at 01:51

5 Answers5

67

Technically the global keyword is used inside a function, not outside.

But you shouldn't really use this approach. See Why global variables are evil. Instead, pass a variable as a parameter:

function($param) {
    $param += 1;
    return $param;
}
$var = 1;
$var = function($var); // get your value of 2
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
Jesse Cohen
  • 4,010
  • 22
  • 25
  • 4
    generally not recommended to use global any more than absolutely necessary –  Feb 20 '11 at 22:24
  • 2
    Even provided access via `global`, the statement `$var + 1;` evaluates to `2` but doesn't store the result anywhere. Try `$var++;` or `$var += 1;`, but note that it changes the original variable as well. – David Harkness Feb 20 '11 at 22:43
16

Globals will do the trick but are generally good to stay away from. In larger programs you can't be certain of there behaviour because they can be changed anywhere in the entire program. And testing code that uses globals becomes very hard.

An alternative is to use a class.

class Counter {
    private $var = 1;

    public function increment() {
        $this->var++;
        return $this->var;
    }
}

$counter = new Counter();
$newvalue = $counter->increment();
Jacob
  • 8,278
  • 1
  • 23
  • 29
  • 1
    +1 for being a far better solution than global state, but it may also be necessary for there to be a getter for $var for cases when somebody only wants the current value without incrementing it – GordonM Nov 08 '16 at 11:01
6
$var = 1;

function() {
  global $var;

  $var += 1;
  return $var;
}

OR

$var = 1;

function() {
  $GLOBALS['var'] += 1;
  return $GLOBALS['var'];
}
Czechnology
  • 14,832
  • 10
  • 62
  • 88
3

This line in your function: $var + 1 will not change the value assigned to $var, even if you use the global keyword.

Either of these will work, however: $var = $var + 1; or $var += 1;

Brian Driscoll
  • 19,373
  • 3
  • 46
  • 65
  • Since you're not answering the question, I think this would have made more sense as a comment. It is correct nevertheless. – devios1 Oct 25 '12 at 07:01
2

See http://php.net/manual/en/language.variables.scope.php for documentation. I think in your specific case you weren't getting results you want because you aren't assigning the $var + 1 operation to anything. The math is performed, and then thrown away, essentially. See below for a working example:

$var = '1';

function addOne() {
   global $var;
   $var = $var + 1;
   return $var;
}
Nathan Anderson
  • 6,768
  • 26
  • 29