Does PHP free local variables immediately after the function ends?

后端 未结 4 1983
迷失自我
迷失自我 2020-12-19 03:59

The code illustrates better what I\'m asking:

function foo(){

  $var = get_huge_amount_of_data();

  return $var[0];
}


$s = foo();

// is memory freed her         


        
4条回答
  •  一向
    一向 (楼主)
    2020-12-19 04:50

    So I know that $var gets freed at some point, but does PHP do it efficiently? Or do I manually need to unset expensive variables?

    Yes, PHP makes a good job. This is a question you should never need to think about. In your case I would rather think about the moment between $var = .. and return .., because that is the moment, where you cannot avoid the memory consumption. You should try to find a solution, where you don't need to fetch the whole dataset via get_huge_amount_of_data() and then select a single item, but only the data you need.

提交回复
热议问题