Home > Backend Development > PHP Tutorial > Static variables How to use php static variables and custom constants

Static variables How to use php static variables and custom constants

WBOY
Release: 2016-07-29 08:41:45
Original
1022 people have browsed it

⚑ 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, but will continue to exist when it is used next time. It can retain the most recent value.
Example below:

Copy code The code is as follows:


function add()
{
static $i=0;
$i++;
echo $i;
}
add( );
echo " ";
add();
?>


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 should be 1 both times. But the actual output is 1 and 2.
This is because the variable i was added with a modifier static when it was 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 it is called for the first time When add, 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 increases itself again and changes from 1 to 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 the variable can be changed arbitrarily while the program is running, but once the custom constant is defined, it can no longer be modified while the program is running.
The definition is as follows:
define("YEAR","2012");
Use the define keyword to bind the string 2012 to YEAR. In the future, 2012 will be used where YEAR appears in the program. Generally, when we define constants, the constant names use uppercase letters.
Example:

Copy code The code is as follows:


define("YEAR","2012");
define("MONTH","12");
define("DATE ","21");
define("THING","Doomsday");
echo YEAR."-".MONTH."-".DATE." ".THING;
?>


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, the difference from variables is that "$" is not used.
The result of its operation is: 2012-12-21 Doomsday.

The above introduces the use of static variables, PHP static variables and custom constants, including the content of static variables. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template