1

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

}
3
  • 1
    $this->msg1 remove $ Commented Aug 4, 2016 at 11:15
  • $msg1 is not global variable. It is public property. Commented Aug 4, 2016 at 11:16
  • does it's working after using @Fast Snail's Solution? Commented Aug 4, 2016 at 11:51

2 Answers 2

2

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

}
Sign up to request clarification or add additional context in comments.

Comments

0

Remove $ from $msg1 in login function

$msg = $this->msg1;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.