you need to use the constructor instead of index ().
class MyController extends CI_Controller { public $variable = array(); function __construct() { parent::__construct(); $this->variable['name'] = "Sam"; $this->variable['age'] = 19; } function index(){ } function another_function(){ print_r($this->variable); } }
If you want to call index() , then call another_function() , try using the CI session class.
class MyController extends CI_Controller { public $variable = array(); function __construct() { parent::__construct(); $this->load->library('session'); if ($this->session->userdata('variable')) { $this->variable = $this->session->userdata('variable'); } } function index(){ $this->variable['name'] = "Sam"; $this->variable['age'] = 19; $this->session->set_userdata('variable', $this->variable); } function another_function(){ print_r($this->variable); } }
tpae
source share