PHP task learning 3: static variables and custom constants _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP task learning 3: static variables and custom constants. How do I declare static variables and use custom constants? what are static variables? Static variables are variables declared using static. The difference between these variables and local variables is that when static variables are declared and used

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:

Copy to ClipboardReference: [www.bkjia.com] 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:

Copy to ClipboardReference: [www.bkjia.com] 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.

Author Blog: http://www.cnblogs.com/walkbro/

Which of the following is the usage of a custom constant of a volume? what is a static variable? Static variables are variables declared using static. The difference between these variables and local variables is that when static variables...

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.