Access a global variable in a PHP function

前端 未结 9 1983
感动是毒
感动是毒 2020-11-29 05:33

According to the most programming languages scope rules, I can access variables that are defined outside of functions inside them, but why doesn\'t this code work?



        
相关标签:
9条回答
  • 2020-11-29 06:04

    It is not working because you have to declare which global variables you'll be accessing:

    $data = 'My data';
    
    function menugen() {
        global $data; // <-- Add this line
    
        echo "[" . $data . "]";
    }
    
    menugen();
    

    Otherwise you can access it as $GLOBALS['data']. See Variable scope.

    Even if a little off-topic, I'd suggest you avoid using globals at all and prefer passing as parameters.

    0 讨论(0)
  • 2020-11-29 06:08

    You can do one of the following:

    <?php
        $data = 'My data';
    
        function menugen() {
            global $data;
            echo "[" . $data . "]";
        }
    
        menugen();
    

    Or

    <?php
        $data = 'My data';
    
        function menugen() {
            echo "[" . $GLOBALS['data'] . "]";
        }
    
        menugen();
    

    That being said, overuse of globals can lead to some poor code. It is usually better to pass in what you need. For example, instead of referencing a global database object you should pass in a handle to the database and act upon that. This is called dependency injection. It makes your life a lot easier when you implement automated testing (which you should).

    0 讨论(0)
  • 2020-11-29 06:09

    Another way to do it:

    <?php
    
    $data = 'My data';
    
    $menugen = function() use ($data) {
    
        echo "[".$data."]";
    };
    
    $menugen();
    

    UPDATE 2020-01-13: requested by Peter Mortensen

    As of PHP 5.3.0 we have anonymous functions support that can create closures. A closure can access the variable which is created outside of its scope.

    In the example, the closure is able to access $data because it was declared in the use clause.

    0 讨论(0)
  • 2020-11-29 06:10

    You need to pass the variable into the function:

    $data = 'My data';
    
    function menugen($data)
    {
        echo $data;
    }
    
    0 讨论(0)
  • 2020-11-29 06:10
    <?php
    
        $data = 'My data';
    
        $menugen = function() use ($data) {
    
            echo "[ $data ]";
        };
    
        $menugen();
    ?>
    

    You can also simplify

    echo "[" . $data . "]"
    

    to

    echo "[$data]"
    
    0 讨论(0)
  • 2020-11-29 06:14

    PHP can be frustrating for this reason. The answers above using global did not work for me, and it took me awhile to figure out the proper use of use.

    This is correct:

    $functionName = function($stuff) use ($globalVar) {
     //do stuff
    }
    $output = $functionName($stuff);
    $otherOutput = $functionName($otherStuff);
    

    This is incorrect:

    function functionName($stuff) use ($globalVar) {
     //do stuff
    }
    $output = functionName($stuff);
    $otherOutput = functionName($otherStuff);
    

    Using your specific example:

        $data = 'My data';
    
        $menugen = function() use ($data) {
            echo "[" . $data . "]";
        }
    
        $menugen();
    
    0 讨论(0)
提交回复
热议问题