6

Is there a way to achieve the following in PHP, or is it simply not allowed? (See commented line below)

function outside() {
    $variable = 'some value';
    inside();
}

function inside() {
    // is it possible to access $variable here without passing it as an argument?
}
4
  • 2
    Even though everyone's telling you you can use global variables: trust me - don't use them. They are best for niche situations only, if you start using them for everything you will find all your code becomes a disaster. Commented Mar 10, 2012 at 12:52
  • If you could explain what you're attempting to accomplish maybe we can come up with either a) cool kludge or b) the proper way to get it done Commented Mar 10, 2012 at 14:01
  • 1
    I came to think of this particular situation while looking into some code, and thought that asking about it would help me get a clearer view of the different scopes. It did! Commented Mar 10, 2012 at 17:29
  • aah, my favorite kind of question, not to get out some code, but only, really oldskool, to learn something ;) Commented Mar 12, 2012 at 8:19

5 Answers 5

3

note that using the global keyword is not advisable, as you have no control (you never know where else in your app the variable is used and altered). but if you are using classes, it'll make things a lot easier!

class myClass {
    var $myVar = 'some value';

    function inside() {
        $this->myVar = 'anothervalue';
        $this->outside(); // echoes 'anothervalue'
    }

    function outside() {
        echo $this->myVar; // anothervalue
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

Its not possible. If $variable is a global variable you could have access it by global keyword. But this is in a function. So you can not access it.

It can be achieved by setting a global variable by$GLOBALS array though. But again, you are utilizing the global context.

function outside() {
    $GLOBALS['variable'] = 'some value';
    inside();
}

function inside() {
        global $variable;
        echo $variable;
}

Comments

1

No, you cannot access the local variable of a function from another function, without passing it as an argument.

You can use global variables for this, but then the variable wouldn't remain local.

Comments

1

It's not possible. You can do it by using global. if you just only do not want to define the parameters but could give it inside the function you can use:

function outside() {
    $variable = 'some value';
    inside(1,2,3);
}

function inside() {
    $arg_list = func_get_args();
    for ($i = 0; $i < $numargs; $i++) {
        echo "Argument $i is: " . $arg_list[$i] . "<br />\n";
    }
}

for that see the php manual funct_get_args()

1 Comment

Didn't know about the funct_get_args() function, could be quite useful in some situations. Thanks!
0

You cannot access the local variable in function. Variable have to set as global

function outside() {
global $variable;
    $variable = 'some value';
    inside();
}

function inside() {
global $variable;
    echo $variable; 
}

This works

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.