php - Unsetting local variables -


there couple of examples of behavior in code igniter framework - creating variable @ beginning of method, working it, , before end of method, unsetting variable.

why unset them? what's point? far know, local variables die @ end of method anyways.

code igniter, session class (see last lines):

function sess_read() {     // fetch cookie     $session = $this->ci->input->cookie($this->sess_cookie_name);      // no cookie?  goodbye cruel world!...     if ($session === false)     {         log_message('debug', 'a session cookie not found.');         return false;     }      // decrypt cookie data     if ($this->sess_encrypt_cookie == true)     {         $session = $this->ci->encrypt->decode($session);     }     else     {         // encryption not used, need check md5 hash         $hash    = substr($session, strlen($session)-32); // last 32 chars         $session = substr($session, 0, strlen($session)-32);          // md5 hash match?  prevent manipulation of session data in userspace         if ($hash !==  md5($session.$this->encryption_key))         {             log_message('error', 'the session cookie data did not match expected. possible hacking attempt.');             $this->sess_destroy();             return false;         }     }      // unserialize session array     $session = $this->_unserialize($session);      // session data unserialized array correct format?     if ( ! is_array($session) or ! isset($session['session_id']) or ! isset($session['ip_address']) or ! isset($session['user_agent']) or ! isset($session['last_activity']))     {         $this->sess_destroy();         return false;     }      // session current?     if (($session['last_activity'] + $this->sess_expiration) < $this->now)     {         $this->sess_destroy();         return false;     }      // ip match?     if ($this->sess_match_ip == true , $session['ip_address'] != $this->ci->input->ip_address())     {         $this->sess_destroy();         return false;     }      // user agent match?     if ($this->sess_match_useragent == true , trim($session['user_agent']) != trim(substr($this->ci->input->user_agent(), 0, 120)))     {         $this->sess_destroy();         return false;     }      // there corresponding session in db?     if ($this->sess_use_database === true)     {         $this->ci->db->where('session_id', $session['session_id']);          if ($this->sess_match_ip == true)         {             $this->ci->db->where('ip_address', $session['ip_address']);         }          if ($this->sess_match_useragent == true)         {             $this->ci->db->where('user_agent', $session['user_agent']);         }          $query = $this->ci->db->get($this->sess_table_name);          // no result?  kill it!         if ($query->num_rows() == 0)         {             $this->sess_destroy();             return false;         }          // there custom data?  if so, add main session array         $row = $query->row();         if (isset($row->user_data) , $row->user_data != '')         {             $custom_data = $this->_unserialize($row->user_data);              if (is_array($custom_data))             {                 foreach ($custom_data $key => $val)                 {                     $session[$key] = $val;                 }             }         }     }      // session valid!     $this->userdata = $session;     unset($session);      return true; } 

unset() destroys specified variables. unset() not free memory consumed php script, free use php script itself. not force immediate memory freeing. php's garbage collector when see fits - intention soon, cpu cycles aren't needed anyway, or late before script run out of memory, whatever occurs first.

so, if creating variable of 10m of size 10 times in loop , unsetting (or rewriting) @ end of loop, memory consumption should low 10m + other variables end of loop.

read more

what's better @ freeing memory php: unset() or $var = null

why unset , null working in different ways


Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -