【问题标题】:Giving my function access to outside variable让我的函数访问外部变量
【发布时间】:2020-04-26 12:07:17
【问题描述】:

我在外面有一个数组:

$myArr = array();

我想让我的函数访问它外部的数组,以便它可以向它添加值

function someFuntion(){
    $myVal = //some processing here to determine value of $myVal
    $myArr[] = $myVal;
}

如何为函数赋予变量正确的作用域?

【问题讨论】:

    标签: php function scope


    【解决方案1】:

    默认情况下,当您在函数内部时,您无权访问外部变量。


    如果您希望您的函数能够访问外部变量,则必须在函数内部将其声明为 global

    function someFuntion(){
        global $myArr;
        $myVal = //some processing here to determine value of $myVal
        $myArr[] = $myVal;
    }
    

    更多信息,请参阅Variable scope

    但请注意,使用全局变量不是一个好习惯:这样,您的函数就不再独立了。


    一个更好的主意是让你的函数返回结果

    function someFuntion(){
        $myArr = array();       // At first, you have an empty array
        $myVal = //some processing here to determine value of $myVal
        $myArr[] = $myVal;      // Put that $myVal into the array
        return $myArr;
    }
    

    然后像这样调用函数:

    $result = someFunction();
    


    您的函数也可以接受参数,甚至处理通过引用传递的参数

    function someFuntion(array & $myArr){
        $myVal = //some processing here to determine value of $myVal
        $myArr[] = $myVal;      // Put that $myVal into the array
    }
    

    然后,像这样调用函数:

    $myArr = array( ... );
    someFunction($myArr);  // The function will receive $myArr, and modify it
    

    有了这个:

    • 您的函数接收外部数组作为参数
    • 并且可以修改它,因为它是通过引用传递的。
    • 这比使用全局变量更好:您的函数是一个单元,独立于任何外部代码。


    有关这方面的更多信息,您应该阅读 PHP 手册的 Functions 部分,尤其是以下子部分:

    【讨论】:

    • @Machine :一个很好的问题 ^^ (从那以后,我编辑了我的答案几次以添加更多信息;也许它被否决了,因为起初不够完整。 ..它可能与全球有关,人们不喜欢它...)
    • @Machine Anti-Global 先生@Coronatus 认为完全可行的答案是错误的。而他的 1,662 次代表使他正确......
    • 我阅读了您的初始版本,它仍然非常好,足以获得 +1 票,绝对不值得投反对票。
    • global 方法引用全局变量,而不是外部变量!
    【解决方案2】:
    $foo = 42;
    $bar = function($x = 0) use ($foo){
        return $x + $foo;
    };
    var_dump($bar(10)); // int(52)
    

    更新:现在支持箭头功能,但我会让更多使用它的人来创建答案

    【讨论】:

      【解决方案3】:
      Global $myArr;
      $myArr = array();
      
      function someFuntion(){
          global $myArr;
      
          $myVal = //some processing here to determine value of $myVal
          $myArr[] = $myVal;
      }
      

      请注意,通常人们会远离全局变量,因为它有一些缺点。

      你可以试试这个

      function someFuntion($myArr){
          $myVal = //some processing here to determine value of $myVal
          $myArr[] = $myVal;
          return $myArr;
      }
      $myArr = someFunction($myArr);
      

      这样你就不用依赖 Globals了。

      【讨论】:

      • 函数作用域内的全局就足够了,你是不是故意在'main'作用域中加了一个?好习惯? (从不使用全局变量..)
      • 函数外的“全局”是没有用的,除非整个文件是从另一个函数中包含的。
      【解决方案4】:
      $myArr = array();
      
      function someFuntion(array $myArr) {
          $myVal = //some processing here to determine value of $myVal
          $myArr[] = $myVal;
      
          return $myArr;
      }
      
      $myArr = someFunction($myArr);
      

      【讨论】:

      • 如果有sn-p的解释,这个答案的质量会大大提高。
      【解决方案5】:

      使用全局变量是实现目标的一种可能不太好的方法。

      您可以通过在函数开头添加global $myArr; 来实现。 但是请注意,在大多数情况下,使用全局变量是一个坏主意,并且可能是可以避免的。

      更好的方法是将数组作为参数传递给函数:

      function someFuntion($arr){
          $myVal = //some processing here to determine value of $myVal
          $arr[] = $myVal;
          return $arr;
      }
      
      $myArr = someFunction($myArr);
      

      【讨论】:

        【解决方案6】:

        这真的是关于事情的正确顺序。

        <?php
        
        /*In general(the rule can be broken) code is interpreted left to right
        top to bottom.
        
        If you want a function to be able to use the values you input,
        write the function first. This means the function should be above where
        it is requested in the code. Add some parameters($param). Note it does
        not need to be called $param, I use $value in the example. This can be
        multiple $vars going from left to right i.e($param_1,$param_2), or be an
        array(), or a mix. Just remember left to right. Left values must exist
        before right values.*/
        
        //Example function here
        function foo($value){
            
            return $value[0] + 1;
        
        }
        
        //Optional way to create array
        //$value[0] = 0;
        
        $value = array(0);
        $limit = 10;
        
        while($value[0] < $limit){
            
            //Request the function here as many times as you want
            echo $value[0] = foo($value);
            echo "<br>";
            
        }
        
        //Clean up afterwards
        unset($value,$limit);
        
        ?>
        

        【讨论】:

        • 不相关的代码太多了,在这里很难看到你提出的解决方案。
        猜你喜欢
        • 1970-01-01
        • 2017-05-25
        • 2014-01-29
        • 2011-04-15
        • 2020-10-30
        • 1970-01-01
        • 2020-08-19
        相关资源
        最近更新 更多