0

I have a small problem in a script I can't solve on my own. Having studied the PHP documentation regarding variable scope, I am not sure if this is even possible.

Take the following example:

function my_funct_a() {
    // Do stuff
    return $a;
}

function my_funct_b() {
    // Do other stuff
    return $a + $b;
}

You see, the problem is that in my_funct_b, $a is not available because it was declared (and is returned) in my_funct_a.

Normally I would pass this variable as an argument, but this is not possible at this point due to some kind of framework limitation.

So I tried to do it like this:

function my_funct_a() {
    // Do stuff
    global $a;
    return $a;
}

function my_funct_b() {
    // Do other stuff
    global $a;
    return $a + $b;
}

This also didn't work, I think because global works 'the other way around'. Instead of declaring a variable as global inside a function to be available outside the function, it has to be declared as global outside the function to be available inside the function.

The Problem is that the value of $a is created in my_funct_a, so I can't global it before the value is known.
Because of that, I tried to do it like this:

// global variable, but no value assigned yet
global $a

function my_funct_a() {
   // Do stuff
   global $a;
   $a = 1; 
   return $a;
}

function my_funct_b() {
   // Do other stuff
   global $a;
   return $a + $b;
}

This also didn't work. Why? Is it even possible without passing the variable as an argument?

6
  • Is this a class code? Commented Feb 22, 2014 at 9:13
  • In my_funct_b(), how do you get $b? Same question for my_funct_a() btw. Commented Feb 22, 2014 at 9:13
  • @DonCallisto No,procedural PHP is used in that project. Commented Feb 22, 2014 at 9:13
  • @Jack $a is a dynamic value created inside that function, $b is a string using that dynamic value. I am afraid I have to to it using the two functions Commented Feb 22, 2014 at 9:15
  • 1
    Your understanding of how global works is wrong. You declare it inside the function, and it allows that function to access the global variable with that name. Your second try should have worked. Commented Feb 22, 2014 at 9:22

4 Answers 4

1

The best way to encapsulate data and functionality is a class:

class Foo {

    protected $val = 1;

    public function inc() {
        return $this->val += 1;
    }

    public function dec() {
        return $this->val -= 1;
    }

}

I'm showing this because it should be used in favour of global variables attempt.

However, this should work:

$val = 1;

function inc() {
    global $val;
    return $val += 1;
}


function dec() {
    global $val;
    return $val -= 1;
}
Sign up to request clarification or add additional context in comments.

Comments

0

If you need the value of $a and it can only be created with my_funct_a(), then add this as a dependency inside my_funct_b():

function my_funct_b() 
{
    $a = my_funct_a();
    // Do other stuff

    return $a + $b;
}

1 Comment

Seems the best solution to me, but maybe - as you said before edit - I din't get the real issue ...
0

You put the global declaration inside the function. That causes uses of the variable within that function to refer to a global variable, not a local one. And if two functions both access the same global variable, they'll see the same values.

function my_funct_a() {
    global $a;
    $a = 10;
    return $a;
}

function my_funct_b() {
    $b = 5;
    global $a;
    return $a + $b;
}

echo my_funct_a() . "\n";
echo my_funct_b();

DEMO

Comments

-1
$a = 1;
$b = 2;

function Sum()
{
    global $a, $b;

    $b = $a + $b;
} 

Sum();
echo $b;

The above script will output 3. By declaring $a and $b global within the function, all references to either variable will refer to the global version.

http://www.php.net/manual/en/language.variables.scope.php

2 Comments

This isn't what OP's asking for
This is exactly was he is asking about. He shouldn't use 'global' word when declaring global variable (outside functions), and thats it.

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.