PHP remove “reference” from a variable.

后端 未结 4 2090
旧巷少年郎
旧巷少年郎 2021-01-11 14:18

I have the code below. I want to change $b to use it again with values. If I do so it changes $a as well. How can I assign a value to $b again after previously assigning it

4条回答
  •  春和景丽
    2021-01-11 14:45

    The answer by @xdazz is correct, but just to add the following great example from the PHP Manual which gives an insight into what PHP is doing under the hood.

    In this example you can see that $bar within the function foo() is a static reference to a function scope variable.

    Unsetting $bar removes the reference but doesn't deallocate the memory:

    
    

    The above example will output:

    Before unset: 1, after unset: 23
    Before unset: 2, after unset: 23
    Before unset: 3, after unset: 23
    

提交回复
热议问题