Use of static and custom constants in php

Source: Internet
Author: User
Welcome to the Linux community forum and interact with 2 million technical staff to access the use of static and custom constants in php: 1. static variables in php are declared using static variables. The difference between such variables and local variables is that when a static variable leaves its scope, its values will not automatically die out, instead, it will continue to exist.

Welcome to the Linux community forum and interact with 2 million technical staff> enter the use of static and custom constants in php: 1. static variables in php are declared using static variables. The difference between such variables and local variables is that when a static variable leaves its scope, its values will not automatically die out, instead, it will continue to exist.

Welcome to the Linux community forum and interact with 2 million technicians>

Use of static and custom constants in php:

1. static variables in php are declared using static variables. The difference between such variables and local variables is that when a static variable leaves its scope, its values will not automatically die out, instead, it continues to exist. When you use it again next time, you can retain the last value.

For example:

Function add ()

{

Static $ I = 0;

$ I ++;

Echo $ I;

}

Add ();

Echo "";

Add ();

?>

In the code above, a function add () is defined, and the add () function is called twice ().

If the code is divided by local variables, 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.

2. A custom constant in php represents another object with a character identifier. 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", "2010 ");

Use the define keyword to bind the 2010 string to the YEAR, and then use 2010 in place of the *** current YEAR in the program. in general, when we define constants, the constant names use uppercase letters.

Example:

The Code is as follows:

Define ("YEAR", "2010 ");

Define ("MONTH", "10 ");

Define ("DATE", "21 ");

Define ("THING", "Doomsday ");

Echo YEAR. "-". MONTH. "-". DATE. "". THING;

?>

In this program, four constants, YEAR, MONTH, DATE, and THING, are defined, and their corresponding values are: 2010,10, 21, Doomsday, when we use echo to connect them and display them, the difference is that "$" is not used ".

The running result is: 2010-10-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.