Does PHP free local variables immediately after the function ends?

后端 未结 4 1988
迷失自我
迷失自我 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条回答
  •  旧时难觅i
    2020-12-19 04:41

    You can see this example on a class, that's because you can "catch" freeing a variable in class' destructor:

    class a {
      function __destruct(){
        echo "destructor
    "; } } function b(){ // test function $c=new a(); echo 'exit from function b()
    '; } echo "before b()
    "; b(); echo "after b()
    "; die();

    This script outputs:

    before b()
    exit from function b()
    destructor
    after b()
    

    So it is now clear that variables are destroyed at function exit.

提交回复
热议问题