Differences after adding static keywords to variables and functions in PHP _php tutorial

Source: Internet
Author: User
Tags smarty template
Static global variable and normal global variable, static local variable and ordinary local variable, static function and ordinary function difference? The following article will answer for you.
  
(1) The description of the global variable (external variable) is then prefixed with static, which constitutes a global variable. Global variables themselves are static storage, and static global variables are, of course, static storage methods. The two are not different in how they are stored. The difference between the two is that the scope of the non-static global variable is the entire source program, when a source program consists of multiple source files, non-static global variables are valid in each source file. A static global variable restricts its scope, which is valid only within the source file that defines the variable, and cannot be used in other source files of the same source program. Because the scope of a static global variable is limited to one source file, it can be common only for functions within that source file, so you avoid causing errors in other source files.

(2) from the above analysis, it can be seen that changing the local variable to a static variable changes its storage mode, which changes its life time. Changing a global variable to a static variable changes its scope and limits its scope of use.

(3) The static function differs from the normal function scope only in this file. Functions that are used only in the current source file should be described as intrinsic (static) and intrinsic functions should be described and defined in the current source file. For functions that can be used outside of the current source file, it should be stated in a header file that the source file to use these functions should contain the header file.

Sum up:

What is the difference between a static global variable and a normal global variable?

The static global variable is only initialized once, preventing it from being referenced in other file units;
 
What is the difference between a static local variable and a normal local variable?

The static local variable is initialized only once, the next time based on the last result value;
 
What is the difference between a static function and a normal function?

The static function has only one copy in memory, and the normal function maintains one copy in each invocation;

Articles you may be interested in

    • Usage and differences in PHP that jump out of multiple loops using Break,continue,goto,return,exit
    • PHP to add a backslash in front of the reason and PHP to remove the backslash method, three ways to close the PHP magic quotes
    • PHP $this, Static, final, const, self, and several other key words to use
    • Use PHP function memory_get_usage to get the current PHP memory consumption to achieve program performance optimization
    • PHP string substitution function str_replace faster than preg_replace
    • Use PHP functions in Smarty Templates and how to use multiple functions for a variable in a smarty template
    • String processing function in PHP (string Functions) Full summary
    • PHP compressed HTML page code reduces network data transfer, clear spaces, tabs, comment tags

http://www.bkjia.com/PHPjc/764162.html www.bkjia.com true http://www.bkjia.com/PHPjc/764162.html techarticle Static global variable and normal global variable, static local variable and ordinary local variable, static function and ordinary function difference? The following article will answer for you. (1) Global variables (external ...

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