Search code examples
phpcodeigniterscopecontrollerglobal-variables

How to call a Global variable in controller method using CodeIgniter


I have created a global variable in the controller and then I used this variable in a method and after some process, this variable has a value, and now I want to access this variable with this value in another method IS that possible or not please tell me the solution. And if it is not the right method then please tell me how I can access a variable from one method to another in the controller My Variable is $variable and the function is:

`public function date_to_db($ui_date){
   $temp = explode('/', $ui_date);
$db_date = $temp[2] . '-' . $temp[1] . '-' . $temp[0];
return $db_date; }`

Solution

  • You can use

    $variable = 'test';
    
    function test()
    {
        global $variable;
    
        $value = $variable;
    
        return $value;  
    
    }
    
    

    or add an index to the global array $GLOBALS['var'] = 'test';