PHP constant declaration and definition method using-php constants

Source: Internet
Author: User
Tags echo message
How to declare PHP constants

Constants can be understood as unchanging quantities. Constants are defined and cannot be changed anywhere else in the script. A constant is made up of English letters, underscores, and math. But the numbers do not appear as initial letters.

Use the Define () function in PHP to define constants in the following syntax format:

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

Parameter description:

Constant_name Required parameters. The constant name, or identifier.
Value Required parameters. The value of the constant.
Case_sensitive Optional parameters. Specifies whether case-sensitive, which is set to true, indicates insensitivity.


There are two ways to get constants: one is to use constants directly, the other is to use the constant () function, and the constant () function is the same as using the constant name directly. However, the function can dynamically output different constants, which is more flexible and convenient to use. The function syntax format is:

MIXDE constant (String const_name)

The parameter const_name is the name of the constant to get, or a variable that stores the constant name. If successful, returns a constant value, otherwise the error message constant is not defined.

To determine whether a constant has been defined, you can use the Define () function. The function syntax format is:

bool Defined (string constant_name)

The parameter constant_name is the name to get the constant, returns true successfully, otherwise false.

Examples of use of PHP constants:

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

<?php    define ("MESSAGE", "can see Once");    The Echo MESSAGE. <br > ";    The Echo Message. <br > ";    The output message indicates that there is no constant    define ("COUNT", "can see multiple times", true);    echo COUNT. " <br > ";    echo CoUnT. " <br > ";      Output count, set case insensitive    $name = "Count";    Get    The constant Echo constant ($name) by using the constant () function. " <br > ";    Whether there is a constant "message"    Echo (Defined ("message")). " <br > ";  Returns true if the constant is defined, using the Echo output to display 1.?>

Output Result:

Can see one time notice:use of undefined constant message-assumed ' Message ' in editor code error location. The message can be seen multiple times to see multiple times 1

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.