What does this function mean? Solution

Source: Internet
Author: User
What does this function mean? Read and set the configuration items (the meaning of the comment here is read and set. I cannot figure out how to set and read it. thank you !!!) PHPcodefunctionC ($ namenull, $ valuenull) {static $ configarray (); configuration item if (is_null ($ name) {what does this function mean?
// Read and set the configuration items (the meaning of the comment here is read and set. I cannot figure out how to set and read it. thank you !!!)
PHP code
  Function C ($ name = null, $ value = null) {static $ config = array (); // configuration item if (is_null ($ name) {return $ config ;} if (! Is_null ($ name) {if (is_string ($ name) {$ name = strtolower ($ name); if (is_null ($ value )) {return isset ($ config [$ name])? $ Config [$ name]: false;} $ config [$ name] = $ value;} elseif (is_array ($ name) {$ name = array_change_key_case ($ name, CASE_LOWER); $ config = array_merge ($ config, $ name );}}}


------ Solution --------------------
The trick is the use of static $ config. when it leaves the function scope, its value will not disappear, and the last value will be retained.
This makes it clear:
PHP code
C ("hello", "I say hello"); // a value of c (array ("a", "B ")); // The second value var_dump (c (); array (3) {["hello"] => string (11) "I say hello" [0] => string (1) "a" [1] => string (1) "B "}

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.