Open In App

How to set or get the config variables in Codeigniter ?

Improve
Improve
Like Article
Like
Save
Share
Report

Config Class: The Config class provides means to retrieve configuration preferences. The config items in Codeigniter can be set and get in the environment. The config value $this->config can be used to get and set items in the environment. The config items are contained within an array, namely, $config. The config file is stored at “application/config/config.php“, which may provide opportunities to add new items or create a separate entity of configuration items.

CodeIgniter by default loads the primary config file (application/config/config.php), whereas the custom files need to be externally loaded.

Setting config variable value: The config variable in CodeIgniter can be set in the environment using the following two methods. The config class method set_item() is used to set the value of a variable in Codeigniter. The set_item() can be used to dynamically set a config item or modify an existing one.

Syntax:

$this->config->set_item('item_name', 'item_value');

 

Arguments:

  • item_name: $config array item name to be changed.
  • item_value: The value to be changed.

PHP




<?php
  // Setting the value of config variable
  $this->config->set_item('str', "Hello GFG!");
  echo($this->config->item('str'));
?>


Output:

[1] "Hello GFG!"

The config item can also be modified or initialized using the key-value pair defined in the $config variable. The item value of the config variable can also be set using the key-value pair in CodeIgniter.

PHP




<?php
  $config['str'] = "Hello GFG!";
  echo($this->config->item('str'));
?>


Output:

[1] "Hello GFG!"

Getting config variable value: The config items can also be easily fetched from the CodeIgniter environment. The config class function item() is used to get the value of a  config variable in Codeigniter.

Syntax:

$this->config->item('item_name');

Arguments:

  • item_name: $config array index to be retrieved.

Return Value: This function returns a value of the array index key specified, or NULL if no such key exists.

PHP




<?php
  // Getting the value of config variable
  $this->config->item('str');
?>


Output:

[1] "Hello GFG!"


Last Updated : 28 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads