0

What exactly does the following line of code doing:

$config = &get_config();

Is & operator passing by reference? Also is get_config() a CodeIgniter helper? I couldn't Google the explanation.

Sai Wai Maung
  • 1,479
  • 5
  • 16
  • 26

3 Answers3

3

As Konrad Rudolph said here: https://stackoverflow.com/a/3957588/837765

The & operator tells PHP not to copy the array when passing it to the function. Instead, a reference to the array is passed into the function, thus the function modifies the original array instead of a copy.

get_config() loads the main config.php file in an array and you modify the returns array directly with the & operator.

It's not a helper. Take a look here (to find the get_config() function) : http://www.8tiny.com/source/codeigniter/nav.html?_functions/index.html

Community
  • 1
  • 1
Pier-Alexandre Bouchard
  • 4,937
  • 5
  • 33
  • 67
2

Is & operator passing by reference?

Yes, it is passed by reference so that you can change config array.

Also is get_config() a CodeIgniter helper?

No, it's a core CodeIgniter method which loads the config array defined in application/config/config.php.

You can look at the source here.

Meneer Venus
  • 1,031
  • 2
  • 11
  • 28
1

The & operator is PHP's reference operator.

CodeIgniter is a rather old framework. In older versions of PHP objects used to be passed by value. This meant that PHP was quite wasteful when it came to stuff like memory allocation, which made things slower than they needed to be. To prevent PHP from allocating memory every time you wanted to access an object you would instead use references.

In newer versions of PHP objects and arrays are automatically passed by reference. When it comes to arrays, new memory is only allocated if you change the array.

99 per cent of the time using references is not necessary. PHP will optimize the code for you in a way that makes sense. You should only use references if you understand what they do, and you have a legit reason to use them.

You can find the get_config() function by searching for it in the source code on GitHub.

Sverri M. Olsen
  • 12,362
  • 3
  • 32
  • 50