In this tutorial, we will learn to create Global Variables in CodeIgniter. Now Why Exactly do we use it..?? Its all because Global Variables are available in every scope and holds static information. It makes the information very convenient and simple to update and can probably be modified from anywhere.

Now when it comes to define them in CodeIgniter, there are several ways do that. I’ve listed some of them below:

1.  Create your own file in application/libraries in which class constructor contains an array as an argument. Now create a new file in /application/config with same name as given in application/libraries and declare your global variables in it. Now to use these variables, autoload the newly created library.

2.  Create your own file in application/core and declare the global variables in it. Than in controller you need to extend your file name instead of CI_Controller.

3.  If the Global Variables are true constants, just add them in application/config/constants.php file and name them in all uppercase like the others are defined.

4.  If you want to set application constants create new config file and add the variables. Now load it as $this->config->load(‘filename’); And access those variables as

 $this->config->item(‘variable_name’); .


Steps To Create Global Variables Using First Method:

Step 1:  First of all, open application/libraries and create a custom class name globals.php.  It contains a constructor function which contains an array as an argument.

<?php

class Globals {

//  Pass array as an argument to constructor function
public function __construct($config = array()) {

//  Create associative array from the passed array
foreach ($config as $key => $value) {
$data[$key] = $value;
}

// Make instance of CodeIgniter to use its resources
$CI = & get_instance();

// Load data into CodeIgniter
$CI->load->vars($data);
}

}

?>

Download The Script File

Save the project in your server directory and use the path given below to run the script.

http://localhost/ci_global_variable/

Step 2: Now to make config file, open application/config and create file as globals.php and write the code given below. This file contains the config variable which will be passed as an array to constructor of Globals class where its keys and values are stored in $data.

<?php

// Create customized config variables
$config['web_Address']= 'https://www.formget.com/blog';
$config['title']= 'CodeIgniter Global Variable';

?>

When constructor function loads, it will take the config variables from the config file in order to use these variables anywhere.

Note:  Name of the above file must be same as the class created in libraries folder otherwise the code will not work.

Step 3: But before using these variables we have to autoload the newly created library globals as given below.

$autoload['libraries'] = array('globals');

Now, we are ready to use these global variables for our code.

Controllers : ci_global_variable_tutorial.php

Create controller which loads the view page.

<?php
class CI_Global_Variable_Tutorial extends CI_Controller{
public function __construct() {
parent::__construct();
}
// Load view page
public function index() {
$this->load->view('show_global_variables');
}
}
?>

Views : show_global_variables.php

In view page, we can use global variables according to our need.

<?php
echo "Title of the blog post : ".$title;
echo "<a href='$web_Address'>"."Click here to go to blog page"."</a>";
?>

Conclusion:

So now you know different ways to use Global variable in CodeIgniter. Hope you like the tutorial, keep reading our other blogs posts for more coding tricks. 🙂