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

前端 未结 13 1982
陌清茗
陌清茗 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:55

    It makes a difference with array elements.

    Consider this example

    $a = array('test' => 1);
    $a['test'] = NULL;
    echo "Key test ", array_key_exists('test', $a)? "exists": "does not exist";
    

    Here, the key 'test' still exists. However, in this example

    $a = array('test' => 1);
    unset($a['test']);
    echo "Key test ", array_key_exists('test', $a)? "exists": "does not exist";
    

    the key no longer exists.

    0 讨论(0)
提交回复
热议问题