Define php Constants

Source: Internet
Author: User
Tags constant definition echo message

Constants can be interpreted as variables with unchanged values. After a constant value is defined, it cannot be changed anywhere in the script. A constant consists of letters, underscores, and numbers, but a number cannot be the first letter.
Use the defaine () function in php to define constants. The syntax format of this function is:
Define (string constant_name, mixed value, case_sensitive = true)

This function has three parameters:
Constant_name: A required parameter. It is a constant name, that is, a identifier.
Value: A required parameter. The value of a constant.
Case_sensitive: an optional parameter, which specifies whether it is case sensitive. If it is set to true, it indicates no sensitivity.

You can use either of the following methods to obtain a constant value:
1. directly obtain the value using the constant name;
2. Use the constant () function.

The constant () function outputs the same effect as the constant name function, but the function can dynamically output different constants, which is flexible and convenient to use.

Syntax format:

Mixed constant (string constant_name)

The constant_name parameter is the name of the constant to be obtained. It can also be a variable that stores the constant name.

If the call succeeds, the constant value is returned. If the call fails, the system prompts that the error message constant is not defined.

Determine whether a constant has been defined to use the defined () function. The syntax format of the function is:

Bool defained (string constants_name)

Constant_name is the name of the constant to be obtained. If yes, true is returned; otherwise, false is returned;

In php, you can use predefined constants to obtain information in php. Such as "_ FILE _", "_ LINE _", and "PHP_ OS.

Example:
Copy codeThe Code is as follows:
<? Php
Define ("MESSAGE", "PHP constant definition, case-sensitive constant names ");
Echo MESSAGE. "<br/>"; // The constant MESSAGE is output.
Echo Message. "<br/>"; // output "Message", indicating that this constant does not exist

Define ("MESSAGE2", "PHP constant definition, constant names are case-insensitive", true );
Echo MESSAGE2. "<br/>"; // output constant MESSAGE2
Echo Message2. "<br/>"; // output constant

$ Constant_name = "message2 ";
Echo constant ($ constant_name). "<br/>"; // output constant MESSAGE2
Echo defined ("MESSAGE"). "<br/>"; // if the definition returns true, echo output shows 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.