PHP Task Learning 3: Static variables and custom constants_PHP Tutorial

WBOY
Release: 2016-07-21 14:55:00
Original
905 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. Continue to exist, and the most recent value can be retained when it is used next time.

The following is an example:

Copy to ClipboardLiehuo.Net CodesQuoted content: [www.bkjia.com] 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 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:

Copy to ClipboardLiehuo.Net CodesQuoted content: [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 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/

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/364490.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...
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