Declaration and use of static variables
How to use custom constants
What are static variables?
Static variables refer to variables declared with static. The difference between this type of variable and local variables is that when a static variable leaves its scope, its value will not automatically die. Continue to exist, and the most recent value can be retained when it is used next time.
The following is an example:
In this program, a function add() is mainly defined, and then add() is called twice.
If you use local variables to divide this code, the output of both times should be 1. But the actual output is 1 and 2.
This is because the variable i is added with a modifier static when it is declared, which means that the i variable is a static variable inside the add() function and has the function of memorizing its own value. When add is called for the first time, i becomes 1 due to self-increment. At this time, i remember that it is no longer 0, but 1. When we call add again, i increments itself again, from 1 It became 2. From this, we can see the characteristics of static variables.
What are custom constants?
The so-called custom constant refers to using a character identifier to represent another object. This object can be a numerical value, a string, a Boolean value, etc. Its definition has many similarities with variables. The only difference is that the value of a variable can be changed arbitrarily while the program is running, but once a custom constant is defined, it can no longer be modified while the program is running.
is defined as follows:
define("YEAR","2012");
Use the define keyword to bind the string 2012 to YEAR. Wherever YEAR appears in the program, replace it with 2012. Generally, when we define constants, the constant names use uppercase letters.
Example:
In this program, four constants are defined, namely YEAR, MONTH, DATE, and THING. Their corresponding values are 2012, 12, 21, and Doomsday. When we use echo to connect them and display them, they are The difference with variables is that "$" is not used.
The result of its operation is: 2012-12-21 Doomsday.
Author’s blog: http://www.cnblogs.com/walkbro/