36

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,798
  • 2
  • 19
  • 32
Henrikz
  • 411
  • 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

8 Answers8

64

You'll need to use the global keyword inside your function. http://php.net/manual/en/language.variables.scope.php

EDIT (embarrassed I overlooked this, thanks to the commenters)

...and store the result somewhere

$var = '1';
function() {
    global $var;
    $var += 1;   //are you sure you want to both change the value of $var
    return $var; //and return the value?
}
Jesse Cohen
  • 3,978
  • 21
  • 24
  • 2
    `$var = '1'; function() { global $var; $var + 1; return $var; }` – Joakim Bodin Feb 20 '11 at 22:23
  • 1
    Bah, it's just too easy! Anyway, huge thanks for the quick reply! Cheers mate! – Henrikz Feb 20 '11 at 22:23
  • 3
    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
15

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,153
  • 1
  • 22
  • 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
7
$var = 1;

function() {
  global $var;

  $var += 1;
  return $var;
}

OR

$var = 1;

function() {
  $GLOBALS['var'] += 1;
  return $GLOBALS['var'];
}
Czechnology
  • 14,723
  • 10
  • 60
  • 87
5
$var = '1';
function addOne() use($var) {
   return $var + 1;
}
Chamandeep
  • 167
  • 2
  • 6
3

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,640
  • 24
  • 29
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
  • 18,831
  • 2
  • 45
  • 63
  • 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
0

According to : https://riptutorial.com/php/example/2496/global-variable-best-practices it's better to put

$var = '1';

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

correct me if I got it wrong.

Tim
  • 65
  • 8
-1
<?php
$var = '1';
function x ($var) {
return $var + 1;
}
echo x($var); //2
?>