I'm a beginner in php. I don't know how to explain this program.

Source: Internet
Author: User
I am a beginner in php. I do not understand this program. & lt; html & gt; & nbsp; & lt; head & gt; & nbsp; & lt; title & gt; test & lt;/title & gt; & nbsp; & lt; body & gt; & lt ;? Phpfunct I am a beginner in php. I don't understand this program.

 
Test
 
Function dealdata (& $ value, & $ key)
{
$ Value. = "123 ";
$ Key. = "456 ";
$ Cnt ++;
}

$ Cnt = 0;
$ States = array ("a" => "aa", "B" => "bb", "c" => "cc ");

Echo $ cnt ."
";
Print_r ($ states );

Array_walk ($ states, "dealdata ");

Echo $ cnt ."
";
Print_r ($ states );

?>
 
 
 
 
Question 1: I think $ cnt is global, so I think cnt should be 3 (I learned C ++ before)
Question 2: Why is the key unchanged when the $ states value changes? I have changed both key and value?
The more detailed the information, the better.
------ Solution --------------------
Question 1
Yes, $ cnt is global, but you change its value in the function. it is valid only in the function and is invalid outside the function.

Question 2
You can only modify the value of array_walk to see the manual.

------ Solution --------------------
Question 1: I think $ cnt is global, so I think cnt should be 3 (I learned C ++ before)
Unlike C ++, php shields all external variables in functions.
If an external variable is used in a function, a global statement is required.

Question 2: Why is the key unchanged when the $ states value changes? I have changed both key and value?
The structure of the host cannot be changed in array_walk.
Changing the key is about changing the structure, so it is not allowed.
Changing the value is only valid when the reference is passed.

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.