PHP remove “reference” from a variable.

后端 未结 4 2103
旧巷少年郎
旧巷少年郎 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:48

    See explanation inline

    $a = 1;    // Initialize it
    $b = &$a;  // Now $b and $a becomes same variable with just 2 different names  
    unset($b); // $b name is gone, vanished from the context  But $a is still available  
    $b = 2;    // Now $b is just like a new variable with a new value. Starting new life.
    

提交回复
热议问题