PHP constant description and usage-php constant definition method

Source: Internet
Author: User
Tags constant definition echo message
A constant can be interpreted as a constant volume. After a constant is defined, it cannot be changed anywhere in the script. A constant consists of letters, underscores, and mathematics. However, a number cannot appear as the first letter. Use the define () function in PHP to define constants. the syntax format is as follows: PHP constant declaration method

A constant can be interpreted as a constant volume. After a constant is defined, it cannot be changed anywhere in the script. A constant consists of letters, underscores, and mathematics. However, a number cannot appear as the first letter.

Use the define () function in PHP to define constants. the syntax format is as follows:

define(string constant_name,mixed value,case_sensitive=true)

Parameter description:

Constant_name Required parameter. Constant name, that is, identifier.
Value Required parameter. Constant value.
Case_sensitive Optional. Specifies whether it is case sensitive. if it is set to true, it indicates no sensitivity.


There are two ways to get a constant: one is to get a constant directly; the other is to use the constant () function, and the constant () function outputs the same effect as directly using the constant name. However, functions can dynamically output different constants, making them more flexible and convenient to use. Function syntax format:

mixde constant(string const_name)

The const_name parameter is the name of the constant to be obtained. It can also be a variable that stores the constant name. If the query succeeds, a constant value is returned. Otherwise, the error message constant is not defined.

To determine whether a constant has been defined, use the define () function. Function syntax format:

bool defined(string constant_name)

The constant_name parameter is the name of the constant to be obtained. if the constant is successful, true is returned. otherwise, false is returned.

PHP constant usage example:

To better understand how to define constants, a constant definition instance is provided here. The preceding define () function, constant () function, and defined () function are used in the instance. Use the defined () function to define a constant, use the constant () function to dynamically obtain the value of a constant, and use the define () function to determine whether a constant is defined. The code is as follows:

 "; Echo Message ."
"; // Output Message, indicating that the constant define (" COUNT "," can be seen multiple times ", true); echo COUNT ."
"; Echo CoUnT ."
"; // Output COUNT, case insensitive $ name =" count "; // Obtain the constant echo constant ($ name) through the constant () function )."
"; // Whether the constant" MESSAGE "echo (defined (" MESSAGE ") exists "))."
"; // If a constant is defined, true is returned. 1 is displayed using echo output.?>

Output result:

You can see the error location of the Notice: Use of undefined constant Message-assumed 'message' in editor code. Message can be seen multiple times.

The above is a detailed description of PHP constant declaration and usage-php constant definition method. For more information, see other related articles in the first PHP community!

Related Article

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.