What the config_item function does not understand in the CodeIgniter source code

Source: Internet
Author: User
{Code ...} $ _ config [0] & amp; get_config (); & amp; get_config (Array $ replacearray () & amp; the role of the symbol is not quite understood here. what is the special role of the & amp; symbol ~
if ( ! function_exists('get_config')){    /**     * Loads the main config.php file     *     * This function lets us grab the config file even if the Config class     * hasn't been instantiated yet     *     * @param    array     * @return    array     */    function &get_config(Array $replace = array())    {        static $config;        if (empty($config))        {            $file_path = APPPATH.'config/config.php';            $found = FALSE;            if (file_exists($file_path))            {                $found = TRUE;                require($file_path);            }            // Is the config file in the environment folder?            if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))            {                require($file_path);            }            elseif ( ! $found)            {                set_status_header(503);                echo 'The configuration file does not exist.';                exit(3); // EXIT_CONFIG            }            // Does the $config array exist in the file?            if ( ! isset($config) OR ! is_array($config))            {                set_status_header(503);                echo 'Your config file does not appear to be formatted correctly.';                exit(3); // EXIT_CONFIG            }        }        // Are any values being dynamically added or replaced?        foreach ($replace as $key => $val)        {            $config[$key] = $val;        }        return $config;    }}// ------------------------------------------------------------------------if ( ! function_exists('config_item')){    /**     * Returns the specified config item     *     * @param    string     * @return    mixed     */    function config_item($item)    {        static $_config;        if (empty($_config))        {            // references cannot be directly assigned to static variables, so we use an array            $_config[0] =& get_config();        }        return isset($_config[0][$item]) ? $_config[0][$item] : NULL;    }}

$_config[0] =& get_config();Andfunction &get_config(Array $replace = array())Inside&The role of the symbol is not quite understood = used here&What is the special role of a symbol? = answer ~

Reply content:
if ( ! function_exists('get_config')){    /**     * Loads the main config.php file     *     * This function lets us grab the config file even if the Config class     * hasn't been instantiated yet     *     * @param    array     * @return    array     */    function &get_config(Array $replace = array())    {        static $config;        if (empty($config))        {            $file_path = APPPATH.'config/config.php';            $found = FALSE;            if (file_exists($file_path))            {                $found = TRUE;                require($file_path);            }            // Is the config file in the environment folder?            if (file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))            {                require($file_path);            }            elseif ( ! $found)            {                set_status_header(503);                echo 'The configuration file does not exist.';                exit(3); // EXIT_CONFIG            }            // Does the $config array exist in the file?            if ( ! isset($config) OR ! is_array($config))            {                set_status_header(503);                echo 'Your config file does not appear to be formatted correctly.';                exit(3); // EXIT_CONFIG            }        }        // Are any values being dynamically added or replaced?        foreach ($replace as $key => $val)        {            $config[$key] = $val;        }        return $config;    }}// ------------------------------------------------------------------------if ( ! function_exists('config_item')){    /**     * Returns the specified config item     *     * @param    string     * @return    mixed     */    function config_item($item)    {        static $_config;        if (empty($_config))        {            // references cannot be directly assigned to static variables, so we use an array            $_config[0] =& get_config();        }        return isset($_config[0][$item]) ? $_config[0][$item] : NULL;    }}

$_config[0] =& get_config();Andfunction &get_config(Array $replace = array())Inside&The role of the symbol is not quite understood = used here&What is the special role of a symbol? = answer ~

It can save memory space to a certain extent and indirectly modify the target value.
Reference delivery official documentation: http://www.php.net/manual/zh/language.references.pass.php
Reference back official documentation: http://php.net/manual/zh/language.references.return.php

Here is an extended example of function reference return.

Function & get_config () {static $ config = 0; $ config + = 1; echo sprintf ("config = % d \ n", $ config); return $ config ;} $ config_item = get_config (); $ config_item = 100; $ config_item = get_config (); $ config_item = & get_config (); // note the & $ config_item = 100; $ config_item = get_config (); // output config = 1 config = 2 config = 3 config = 101

Transfers references to save memory.

// They all point to the static global variable $ config's zval $ config1 = & get_config (); $ config2 = & get_config ();

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.