Home  >  Article  >  Backend Development  >  How to use php static variables and custom constants_PHP tutorial

How to use php static variables and custom constants_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:41:21763browse

⚑ 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 variables and local variables is that when a static variable leaves its scope, its value will not automatically die, but will continue to exist. When using it, the most recent value can be retained.
The following example:

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 of both times should be 1. 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 the first When we call add 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 again and changes from 1 to 1. 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. It will be used wherever YEAR appears in the program. 2012 instead. 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;
?>

In this program, four constants are defined, namely YEAR, MONTH, DATE, THING, their corresponding values ​​are 2012, 12, 21, Doomsday respectively. 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321180.htmlTechArticleDeclaration of static variables and 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 static...
Statement:
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