tenten
tenten

Reputation: 1276

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
            }
        }
    }

}

Upvotes: 1

Views: 65

Answers (2)

Rax Shah
Rax Shah

Reputation: 531

Remove $ from $msg1 in login function

$msg = $this->msg1;

Upvotes: 0

Keyur Chavda-kc1994
Keyur Chavda-kc1994

Reputation: 1045

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
        }
    }
}

}

Upvotes: 2

Related Questions