0

I am trying to define a php function inside a codeigniter view and use $data variable in it. For example this is my controller

class Something extends CI_Controller {

        public function index()
        {
           $data = array();
           $data["world"] = "hello world"     
           $this->load->view('something', $data);
        }
}

This is my view something

function doSomething(){
   echo $world;
}
doSomething();

The expected result is hello world.

kingofjong
  • 61
  • 8
  • If you can, then you will need to inject the `$data` into the function (SCOPE) like `doSomething($data);` and the prototype would then be `function doSomething($data){` – RiggsFolly Jan 29 '21 at 17:43
  • Your immediate problem has nothing to do with CodeIgniter views, and everything to do with not understanding variable scope. You may run into _other_ problems defining a function there, like defining the function multiple times with the same name, but you need to understand functions and scope properly before you get there. – IMSoP Jan 29 '21 at 19:30
  • try printing `$data['world']` in view and store it in another variable and access this in your function – Santosh Dangare Feb 02 '21 at 08:26

0 Answers0