Questions about static variables in PHP

Source: Internet
Author: User
Why are the results of this output CCC?

 

But that doesn't 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 tell the principle:)

Reply content:

Why are the results of this output CCC?

   

But that doesn't 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 tell the principle:)

Static is usually used in function or class, and this usage is less common.
I think that when the PHP engine sweeps to static, it will put the top of the mentioned domain in the static declaration

     

It becomes a

      

The next evolved into

       

The third one evolved into

                                                

Static variables are initialized before they are executed. No matter how many times it is initialized, it only works for the last time. You can assign a value without the static.
All of your

static $xxx = XXX;

Only one last sentence is useful.

The following code should be able to help you clarify the idea:

 
          

The result is:

Array (size=2)  0 = String ' gga ' (length=3)  1 = string ' Gaga ' (length=4)

The second second defines the array as empty and should be ignored.
You change the second definition of the first to "' and see the results."

Learn the ...

  • 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.