tenten tenten - 3 months ago 9
PHP Question

Error when calling global variables in codeigniter

I want to declare a global variable with a value and access it in the functions.

I'm done below code but showing Undefined variable: msg1 in the login function.

Please help me to do this.

class loader extends CI_Controller {

public $msg1 = "login error";

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

}

public function login() {
$result = $this->login_model->login();
if (!$result) {
$msg = $this->$msg1;
$this->index($msg) ;
} else {
//something here
}
}
}

}

Answer

Try this,

class loader extends CI_Controller {

public $msg1 = "login error";

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

}

public function login() {
    $result = $this->login_model->login();
    if (!$result) {
        $msg = $this->msg1;
        $this->index($msg) ;
    } else {
        //something here
        }
    }
}

}
Comments