PHP is constantly declaring static variable problems

Source: Internet
Author: User

  
   

输出结果是

如果去掉静态static的话

$arr=array();$arr[]=1;$arr=array();$arr[]=2;$arr=array();$arr[]='haha'; var_dump($arr);

输出结果是:

~,为什么静态变量重复声明了多次,也相当于只声明了一次呢???~

Reply content:


  
    

输出结果是

如果去掉静态static的话

$arr=array();$arr[]=1;$arr=array();$arr[]=2;$arr=array();$arr[]='haha'; var_dump($arr);

输出结果是:

~,为什么静态变量重复声明了多次,也相当于只声明了一次呢???~

Static variable fixed in memory address, non-static variable memory address is not fixed, can be changed

In general, static variables are statically assigned, and their life cycle is the same as the life cycle of the program, which only ends the life cycle of a program when it exits, which, contrary to local variables, In some languages, global variables are also statically assigned. For example, global variables in PHP and JavaScript.

Can read a book Http://www.php-internals.com/book/?p=chapt03/03-04-static-var
will help you

Declaration and assignment are not the same, such as

 !--? Phpstatic $ Arr=array (); $arr []=1;static $arr =array (); $arr []=2;static $arr =array (); $arr = Array (); $arr []= ' haha '; var_dump ($arr);   

run result is:

 " Array (1) {[0]= > String (4) "Haha"}   
  • 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.