1

I looked around but can't really find anything. I tried using global but I think I am using it wrong.

function testing() {
    $a = (object) array('a' => 100, 'b' => 200);
    function test2(){
        global $a;
        var_dump($a);
    }
    test2();
}
testing();

I want to be able to get $a inside test2() without passing the variable as parameter.

EDIT: Thank you for the comments and answers. The examples work however in my specific case it does not seem to work. I wrote this little function at the top of my view, then call it when I need it.

var_dump($data); // DATA here is fine - I need it in the function
function getDataVal($data_index) {
    return (isset($data->{$data_index}))?$data->{$data_index}:'';
}

I then call it on the page a bit later like this:

<input type="text" id="something" value="<?=getDataVal('something')?>" />

I know I can just pass $data in the request, however I was hoping there was an easier way to access data inside that function.

4
  • Because the variable isn't in the global namespace, it's in the first function's. Define it global within both functions, or look at the link below. Commented Feb 26, 2016 at 10:11
  • 3
    See this post: stackoverflow.com/questions/4938170/… Commented Feb 26, 2016 at 10:11
  • Define as global in both functions Commented Feb 26, 2016 at 10:16
  • 1
    Try to avoid using global variables. A global variable is like an open lunchbox left in the middle of the school yard. You leave it there with your lunch inside in the morning and you never know what it will contain at noon. Commented Feb 26, 2016 at 10:21

2 Answers 2

4

global means "global", e.g. a variable defined in the global namespace.

I don't know why you are trying to avoid passing the variable as a parameter. My guess: it should be writeable, and it usually is not.

These are two variants of the same solution:

<?php


// VARIANT 1: Really globally defined variable
$a = false; // namespace: global

function testing1() {
    global $a;
    $a = (object) array('a' => 100, 'b' => 200);

    function test1(){
        global $a;
        echo '<pre>'; var_dump($a); echo '</pre>'; 
    }
    test1();
}
testing1();


// VARIANT 2: Passing variable, writeable
function testing2() {
    $a = (object) array('a' => 100, 'b' => 200);

    function test2(&$a){ // &$a: pointer to variable, so it is writeable
        echo '<pre>'; var_dump($a); echo '</pre>'; 
    }
    test2($a);
}
testing2();


}
testing();

Result, both variants:

object(stdClass)#1 (2) {
  ["a"]=> int(100)
  ["b"]=> int(200)
}

object(stdClass)#2 (2) {
  ["a"]=> int(100)
  ["b"]=> int(200)
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for taking the time to answer my question. I have updated my question.
0

Define it as global variable:

    a = array();
    function testing() {
        global $a;
        $a = (object) array('a' => 100, 'b' => 200);
        function test2(){
            global $a;
            var_dump($a);
        }
        test2();
    }

testing();

Edit Missing $ in global a

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.