Php static variables and custom constants

Source: Internet
Author: User
Php static variables and custom constants⚑Declaration and use of static variables
⚑Usage of custom constants

What are static variables?
Static variables are variables declared using static. The difference between these variables and local variables is that when a static variable leaves its scope, its values will not automatically disappear, instead, it continues to exist. when you use it again next time, you can retain the last value.
The following is an example:
The code is as follows:
Function add ()
{
Static $ I = 0;
$ I ++;
Echo $ I;
}
Add ();
Echo "";
Add ();
?>

In this program, we mainly define a function add (), and then call add () twice ().
If you use local variables to divide the code into one, the two outputs should be 1. But the actual output is 1 and 2.
This is because variable I is declared with a modifier static, which indicates that the variable I is a static variable in the add () function, it has the ability to remember its own values. when I call add for the first time, because auto-increment is changed to 1, I will remember that I am no longer 0, but 1, when we call add again, I changes from 1 to 2. From this, we can see the characteristics of static variables.
What is a custom constant?
A custom constant represents another object with a character. this object can be a value, a string, a Boolean value, and so on. Its definition has many similarities with variables. There is only one difference, that is, the variable value can be changed at will during the program running, and once the custom constant is defined, it can no longer be modified in the program running.
The definition is as follows:
Define ("YEAR", "2012 ");
Use the define keyword to bind the 2012 string to YEAR. in the future, 2012 will be used in place of YEAR in the program. In general, when we define constants, the constant names use uppercase letters.
Example:
The code is as follows:
Define ("YEAR", "2012 ");
Define ("MONTH", "12 ");
Define ("DATE", "21 ");
Define ("THING", "Doomsday ");
Echo YEAR. "-". MONTH. "-". DATE. "". THING;
?>

In this program, four constants, YEAR, MONTH, DATE, and THING, are defined. Their values are, 21, Doomsday, when we use echo to connect them and display them, the difference is that "$" is not used ".
The running result is: 2012-12-21 Doomsday.

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.