Questions about static variables in php

Source: Internet
Author: User
Why are the output results ccc? {Code...} but cannot change the array? {Code...} and this will add elements to the array? {Code...} hopes to explain the principle: :) why are the output results ccc?

 

But this cannot change the array?

  

And this will add elements to the array?

static $arr = array();$arr[] = 'sdsds';$arr[] = 'xxx';var_dump($arr);static $arr = array('ww', 'vv');var_dump($arr);

I hope I can talk about the principle :)

Reply content:

Why are the output results ccc?

   

But this cannot change the array?

    

And this will add elements to the array?

static $arr = array();$arr[] = 'sdsds';$arr[] = 'xxx';var_dump($arr);static $arr = array('ww', 'vv');var_dump($arr);

I hope I can talk about the principle :)

Static is generally used in functions or classes.
I think the php engine will refer the static declaration to the top of the domain when scanning static.

     

It becomes

      

The next step evolved

       

The third has evolved

                                                

Static variables have been initialized before execution. No matter how many times it is initialized, it is only useful for the last time. You can assign values without static values.
All of you

static $xxx = xxx;

Only the last sentence is useful.

The following code can help you clarify your ideas:

         

The result is:

array (size=2)  0 => string 'gga' (length=3)  1 => string 'gaga' (length=4)

When the second array is defined for the second time, It is null and should be ignored.
Change the second definition of the first one to ''to see the result.

Learning...

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.