What's better at freeing memory with PHP: unset() or $var = null

前端 未结 13 2273
陌清茗
陌清茗 2020-11-22 08:57

I realise the second one avoids the overhead of a function call (update, is actually a language construct), but it would be interesting to know if one is be

13条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 09:46

    It works in a different way for variables copied by reference:

    $a = 5;
    $b = &$a;
    unset($b); // just say $b should not point to any variable
    print $a; // 5
    
    $a = 5;
    $b = &$a;
    $b = null; // rewrites value of $b (and $a)
    print $a; // nothing, because $a = null
    

提交回复
热议问题