Differences between variables and functions in php after the static keyword is added _ PHP Tutorial

Source: Internet
Author: User
Tags exit in
The difference between variables and functions in php after the static keyword is added. What are the differences between static global variables and common global variables, static local variables and common local variables? This article will answer your questions one by one. (1) What are the differences between global variables (external static global variables and common global variables, static local variables and common local variables) and static functions? This article will answer your questions one by one.
  
(1) the description of global variables (external variables) is preceded by static to form a static global variable. Global variables are static storage, and static global variables are also static storage. The two are not different in storage methods. The difference between the two lies in that the scope of non-static global variables is the entire source program. when a source program is composed of multiple source files, non-static global variables are valid in each source file. The static global variable limits its scope, that is, it is valid only in the source file defining the variable, and cannot be used in other source files of the same source program. Because the scope of static global variables is limited to one source file, they can only be shared by functions in the source file. Therefore, errors in other source files can be avoided.

(2) from the above analysis, we can see that after changing a local variable to a static variable changes its storage mode, that is, it changes its survival. After changing a global variable to a static variable, it changes its scope and limits its scope of use.

(3) static functions and common functions have different scopes, only in this file. Only functions used in the current source file should be declared as internal functions (static). internal functions should be described and defined in the current source file. For functions that can be used outside the current source file, it should be described in a header file that the source files to use these functions must contain this header file.

To sum up:

What is the difference between static global variables and common global variables?

Static global variables are modified only once to prevent being referenced in other file units;
 
What is the difference between static local variables and common local variables?

Static local variables are initialized only once, and the next time is based on the previous result value;
 
What is the difference between a static function and a common function?

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

Articles you may be interested in
  • Use break, continue, goto, return, and exit in multiple loops in PHP
  • Reasons why PHP adds a backslash before the quotation mark and how PHP removes the backslash, there are three ways to disable the php magic quotation mark
  • Usage of several keywords such as $ this, static, final, const, and self in php
  • Use the PHP function memory_get_usage to obtain the current PHP memory consumption for program performance optimization.
  • Php string replacement function str_replace is faster than preg_replace
  • Use php functions in the smarty template and how to use multiple functions for a variable in the smarty Template
  • Summary of String Functions in PHP
  • PHP compresses html webpage code to reduce the amount of network data transmitted, clear spaces, tabs, and comment mark

Why? This article will answer your questions one by one. (1) global variables (outside...

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.