CodeIgniter Forums
global variable in controllers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: global variable in controllers (/thread-52282.html)



global variable in controllers - El Forum - 06-04-2012

[eluser]brian88[/eluser]
For my controller, I was wondering how to use the same variable for more than 1 function.
Code:
class Main extends CI_Controller {
  function __construct() {
    parent::__construct();
    $v = "23";
  }

  function index() {
    echo $v; // doesnt work, undefined var
  }
}



global variable in controllers - El Forum - 06-04-2012

[eluser]Learn CodeIgniter[/eluser]
Code:
class Main extends CI_Controller {

  public $v;

  function __construct()
  {
    parent::__construct();

    $this->v = "23";
  }

  function index()
  {
    echo $this->v; // doesnt work, undefined var
  }
}