Help me understand PHP variable references and scope

References:

  • If I pass a variable to a function (eg $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?
  • Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?
  • If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.
  • I think can understand passing by reference (eg &$var) correctly by knowing how this works, first.

    Scope:

  • What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?
  • Similarly, does declaring in array in a function called within a function allow it to be available in the caller?
  • If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?
  • PHP is so much fun. :(


    If I pass a variable to a function (eg $var), is that supposed to be a copy of a reference to the actual variable (such that setting it null doesn't affect other copies)?

    Depends on the function. And also how you call it. Look at this example: http://www.ideone.com/LueFc

    Or is it receiving a reference to what is a new copy of the actual variable (such that setting it to null destroys its copy only)?

    Again depends on the function

    If the latter, does this copy objects and arrays in memory? That seems like a good way to waste memory and CPU time, if so.

    Its going to save memory to use a reference, certainly. In php>4 it always uses reference for objects unless you specify otherwise.

    What's the deal with local scope? Am I right in observing that I can declare an array in one function and then use that array in other functions called within that function WITHOUT passing it to them as a parameter?

    No you can't.

    Similarly, does declaring in array in a function called within a function allow it to be available in the caller?

    No, it doesn't.

    If not, does scoping work by a call stack or whatever like every bloody thing I've come to understand about programming tells me it should?

    If you want to use a variable from outside the function, before using it, you'd write global $outsidevar


    Concerning your first set of questions:

    foo($a);
    function foo($b) { echo $b; }
    

    In this case, $a will not be copied to a new variable $b , only because it is passed by value.

    This is because PHP uses the copy-on-write concept. PHP will not copy the contents of a variable, unless they are changed. Instead PHP will increment the refcount property of the existing "zval" of $a .

    Well, the whole thing is not that trivial, but to answer your question: No, it does not copy the variable, unless you write to it in the function and no, you won't save CPU and Memory by using a reference. In most cases the reference won't change performance at all, but in the worst case it will actually degrade it (because if a not is_ref variant of the variable already exists and a reference is created the value of the variable must be copied to get a zval with is_ref and one without). Optimizing code by using references is no good.


    if argument to a function is defined as so "function my_function($variable) {}" then you are getting a copy of the variable and any alterations made to the variable inside your function will not be available to the function caller. you can pass a variable by reference by prepending an ampersand to the argument when defining your function and thus any alterations made to the variable will persist to the function caller, ie "function my_function(&$variable) {}"

    function myfunction($var) {
        $var = 'World';
    }
    $var = 'Hello';
    myfunction($var);
    echo $var; // 'Hello';
    

    Passing a variable by reference

    function myfunction(&$var) {
        $var = 'World';
    }
    $var = 'Hello';
    myfunction($var);
    echo $var; // 'World'
    
    链接地址: http://www.djcxy.com/p/50096.html

    上一篇: iPhone和/或Android手机是否有良好的OCR API?

    下一篇: 帮助我了解PHP变量引用和范围